Skip to main content
summaryrefslogtreecommitdiffstats
blob: cd32ac1271db003ba225f610acfa194a7f74f6af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.core;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.cdt.core.resources.ACBuilder;
import org.eclipse.cdt.utils.CygPath;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class ErrorParserManager extends OutputStream {

	private int nOpens;

	private final static String OLD_PREF_ERROR_PARSER = "errorOutputParser"; //$NON-NLS-1$
	public final static String PREF_ERROR_PARSER = CCorePlugin.PLUGIN_ID + ".errorOutputParser"; //$NON-NLS-1$

	private IProject fProject;
	private IMarkerGenerator fMarkerGenerator;
	// Maps a file name without directory to a IFile object or a list of a IFile objects. 
	private Map<String, Object> fFilesInProject;	// Files or lists of files keyed by the file name

	private Map<String, IErrorParser[]> fErrorParsers;
	private ArrayList<ProblemMarkerInfo> fErrors;

	private Vector<IPath> fDirectoryStack;
	private IPath fBaseDirectory;

	private String previousLine;
	private OutputStream outputStream;
	private StringBuffer currentLine = new StringBuffer();

	private StringBuffer scratchBuffer = new StringBuffer();
	
	private boolean hasErrors = false;

	public ErrorParserManager(ACBuilder builder) {
		this(builder.getProject(), builder);
	}

	public ErrorParserManager(IProject project, IMarkerGenerator markerGenerator) {
		this(project, markerGenerator, null);
	}

	public ErrorParserManager(IProject project, IMarkerGenerator markerGenerator, String[] parsersIDs) {
		this(project, project.getLocation(), markerGenerator, parsersIDs);
	}

	public ErrorParserManager(IProject project, IPath workingDirectory, IMarkerGenerator markerGenerator, String[] parsersIDs) {
		fProject = project;
		if (parsersIDs == null) {
			enableAllParsers();
		} else {
			fErrorParsers = new LinkedHashMap<String, IErrorParser[]>(parsersIDs.length);
			for (String parsersID : parsersIDs) {
				IErrorParser[] parsers = CCorePlugin.getDefault().getErrorParser(parsersID);
				fErrorParsers.put(parsersID, parsers);
			}
		}
		fMarkerGenerator = markerGenerator;
		initErrorParserManager(workingDirectory);
	}

	private void initErrorParserManager(IPath workingDirectory) {
		fFilesInProject = new HashMap<String, Object>();
		fDirectoryStack = new Vector<IPath>();
		fErrors = new ArrayList<ProblemMarkerInfo>();

		List<IResource> collectedFiles = new ArrayList<IResource>();
		fBaseDirectory = (workingDirectory == null || workingDirectory.isEmpty()) ? fProject.getLocation() : workingDirectory;
		collectFiles(fProject, collectedFiles);

		for (int i = 0; i < collectedFiles.size(); i++) {
			IFile file = (IFile) collectedFiles.get(i);
			String filename = file.getName();
			Object existing = fFilesInProject.put(filename, file);
			if (existing != null) {
				Collection<IFile> files;
				if (existing instanceof IFile) {
					files = new ArrayList<IFile>();
					files.add((IFile) existing);
				} else {
					@SuppressWarnings("unchecked")
					final Collection<IFile> casted = (Collection<IFile>) existing;
					files = casted;
				}
				files.add(file);
				fFilesInProject.put(filename, files);
			}
		}
	}

	public IProject getProject() {
		return fProject;
	}
	
	public IPath getWorkingDirectory() {
		if (fDirectoryStack.size() != 0) {
			return fDirectoryStack.lastElement();
		}
		// Fall back to the Project Location
		return fBaseDirectory;
	}

	public void pushDirectory(IPath dir) {
		if (dir != null) {
			IPath pwd = null;
			if (fBaseDirectory.isPrefixOf(dir)) {
				int segments = fBaseDirectory.matchingFirstSegments(dir);
				pwd = dir.removeFirstSegments(segments);
			} else {
				pwd = dir;
			}
			fDirectoryStack.addElement(pwd);
		}
	}

	public IPath popDirectory() {
		int i = fDirectoryStack.size();
		if (i != 0) {
			IPath dir = fDirectoryStack.lastElement();
			fDirectoryStack.removeElementAt(i - 1);
			return dir;
		}
		return new Path(""); //$NON-NLS-1$
	}

	public int getDirectoryLevel() {
		return fDirectoryStack.size();
	}

	private void enableAllParsers() {
		fErrorParsers = new LinkedHashMap<String, IErrorParser[]>();
		String[] parserIDs = CCorePlugin.getDefault().getAllErrorParsersIDs();
		for (String parserID : parserIDs) {
			IErrorParser[] parsers = CCorePlugin.getDefault().getErrorParser(parserID);
			fErrorParsers.put(parserID, parsers);
		}
		if (fErrorParsers.size() == 0) {
			initErrorParsersMap();
			CCorePlugin.getDefault().getPluginPreferences().setValue(OLD_PREF_ERROR_PARSER, ""); // remove old prefs //$NON-NLS-1$
		}
	}

	private void initErrorParsersMap() {
		String[] parserIDs = CCorePlugin.getDefault().getAllErrorParsersIDs();
		for (String parserID : parserIDs) {
			IErrorParser[] parsers = CCorePlugin.getDefault().getErrorParser(parserID);
			fErrorParsers.put(parserID, parsers);
		}
	}

	protected void collectFiles(IProject parent, final List<IResource> result) {
		try {
			parent.accept(new IResourceProxyVisitor() {
				public boolean visit(IResourceProxy proxy) {
					if (proxy.getType() == IResource.FILE) {
						result.add(proxy.requestResource());
						return false;
					}
					return true;
				}
			}, IResource.NONE);
		} catch (CoreException e) {
			CCorePlugin.log(e.getStatus());
		}
	}

	/**
	 * Parses the input and try to generate error or warning markers
	 */
	private void processLine(String line) {
		if (fErrorParsers.size() == 0)
			return;

		// If the line is too long, it is most likely a command line and not an error message
		// Don't process it since it'll probably be really slow and won't find an error anyway
		if (line.length() > 1000)
			return;
		
		String[] parserIDs = new String[fErrorParsers.size()];
		Iterator<String> items = fErrorParsers.keySet().iterator();
		for (int i = 0; items.hasNext(); i++) {
			parserIDs[i] = items.next();
		}

		for (int i = 0; i < parserIDs.length; ++i) {
			IErrorParser[] parsers = fErrorParsers.get(parserIDs[i]);
			for (IErrorParser curr : parsers) {
				if (curr.processLine(line, this)) {
					return;
				}
			}
		}
	}

	/**
	 * Returns the project file with the given name if that file can be uniquely identified.
	 * Otherwise returns <code>null</code>. 
	 */
    public IFile findFileName(String fileName) {
		IPath path = new Path(fileName);
		Object obj = fFilesInProject.get(path.lastSegment());
		if (obj == null) {
			return null;
		}
		if (obj instanceof IFile) {
			IFile file = (IFile) obj;
			if (isPossibleMatch(path, file)) {
				return file;
			}
			return null;
		}
        IFile matchingFile = null;
        @SuppressWarnings("unchecked")
		Collection<IFile> files = (Collection<IFile>) obj;
		for (IFile file : files) {
			if (isPossibleMatch(path, file)) {
				if (matchingFile != null) {
					return null;	// Ambiguous match
				}
				matchingFile = file;
			}
		}
		
		if(matchingFile == null) {
			// one more attempt... look for a file with that name in the project
			Object candidateFile = fFilesInProject.get(path.lastSegment());
			if (candidateFile instanceof IFile) {
				return (IFile) candidateFile;
			}
			// not found or getting list of files which is ambiguous 
			return null;
		}
		
		return matchingFile;
	}

	/**
	 * Checks if a file system path {@code location} may point to a workspace {@code resource}.
	 * @param location an absolute or relative file system path.
	 * @param resource a workspace resource.
	 * @return {@code true} if {@code location} may point to {@code resource}.
	 */
	private static boolean isPossibleMatch(IPath location, IResource resource) {
		IPath resourceLocation = resource.getLocation();
		if (resourceLocation == null) {
			// could be an EFS path
			URI locationURI = resource.getLocationURI();
			if(locationURI == null)
				return false;
			
			// Use the path information from the URI to see if the path as seen by the build
			// matches the path that the resource points to.
			
			// This relies on the assumption that the EFS filesystem being used stores path information in the path
			// portion of the URI.  This may not be in fact the case, but most filesystems adhere to such a format.
			String path = locationURI.getPath();
			
			if(path == null)
				return false;
			
			if(location.isAbsolute())
				return location.toString().equals(path);
			
			IPath uriPath = new Path(path);
			int prefixLen = uriPath.segmentCount() - location.segmentCount(); 
			return prefixLen >= 0 && uriPath.removeFirstSegments(prefixLen).equals(location);
			
		}
		if (location.getDevice()==null) {
			resourceLocation = resourceLocation.setDevice(null);
		}
		if (location.isAbsolute()) {
			return location.equals(resourceLocation);
		} else {
			int prefixLen = resourceLocation.segmentCount() - location.segmentCount(); 
			return prefixLen >= 0 && resourceLocation.removeFirstSegments(prefixLen).equals(location);
		}
	}

	protected IFile findFileInWorkspace(IPath path) {
		IFile file = null;
		if (path.isAbsolute()) {
			IWorkspaceRoot root = fProject.getWorkspace().getRoot();
			file =  root.getFileForLocation(path);
			// It may be a link resource so we must check it also.
			if (file == null) {
				IFile[] files = root.findFilesForLocation(path);
				for (IFile file2 : files) {
					if (file2.getProject().equals(fProject)) {
						file = file2;
						break;
					}
				}
			}

		} else {
			file = fProject.getFile(path);
		}
		return file;
	}

	/**
	 * Returns <code>true</code> if the project contains more than one file with the given name.
	 */
	public boolean isConflictingName(String fileName) {
		IPath path = new Path(fileName);
		Object obj = fFilesInProject.get(path.lastSegment());
		return obj != null && !(obj instanceof IFile);
	}

	/**
	 * Called by the error parsers.
	 */
	public IFile findFilePath(String filePath) {
		IPath path = null;
		IPath fp = new Path(filePath);
		if (fp.isAbsolute()) {
			if (fBaseDirectory.isPrefixOf(fp)) {
				int segments = fBaseDirectory.matchingFirstSegments(fp);
				path = fp.removeFirstSegments(segments);
			} else {
				path = fp;
			}
		} else {
			path = getWorkingDirectory().append(filePath);
		}

		IFile file = null;
		// The workspace may throw an IllegalArgumentException
		// Catch it and the parser should fallback to scan the entire project.
		try {
			file = findFileInWorkspace(path);
		} catch (Exception e) {
		}

		// That didn't work, see if it is a cygpath
		if (file == null) {
			CygPath cygpath = null;
			try {
				cygpath = new CygPath();
				fp = new Path(cygpath.getFileName(filePath));
				if (fBaseDirectory.isPrefixOf(fp)) {
					int segments = fBaseDirectory.matchingFirstSegments(fp);
					path = fp.removeFirstSegments(segments);
				} else {
					path = fp;
				}
				file = findFileInWorkspace(path);
			} catch (Exception e) {
			}
			finally {
				if (cygpath != null)
					cygpath.dispose();
			}
		}
		
		// We have to do another try, on Windows for cases like "TEST.C" vs "test.c"
		// We use the java.io.File canonical path.
		if (file == null || !file.exists()) {
			File f = path.toFile();
			try {
				String canon = f.getCanonicalPath();
				path = new Path(canon);
				file = findFileInWorkspace(path);
			} catch (IOException e1) {
			}
		}
		return (file != null && file.exists()) ? file : null;
	}

	/**
	 * Called by the error parsers.
	 */
	public void generateMarker(IResource file, int lineNumber, String desc, int severity, String varName) {
		generateExternalMarker(file, lineNumber, desc, severity, varName, null);
	}

	/**
	 * Called by the error parsers for external problem markers
	 */
	public void generateExternalMarker(IResource file, int lineNumber, String desc, int severity, String varName, IPath externalPath) {
		ProblemMarkerInfo problemMarkerInfo = new ProblemMarkerInfo(file, lineNumber, desc, severity, varName, externalPath);
		fErrors.add(problemMarkerInfo);
		if (severity == IMarkerGenerator.SEVERITY_ERROR_RESOURCE)
			hasErrors = true;
	}

	/**
	 * Called by the error parsers.  Return the previous line, save in the working buffer.
	 */
	public String getPreviousLine() {
		return new String((previousLine) == null ? "" : previousLine); //$NON-NLS-1$
	}

	/**
	 * Method setOutputStream.
	 * @param os
	 */
	public void setOutputStream(OutputStream os) {
		outputStream = os;
	}

	/**
	 * Method getOutputStream. It has a reference count
	 * the stream must be close the same number of time this method was call.
	 * @return OutputStream
	 */
	public OutputStream getOutputStream() {
		nOpens++;
		return this;
	}

	/**
	 * @see java.io.OutputStream#close()
	 */
	@Override
	public void close() throws IOException {
		if (nOpens > 0 && --nOpens == 0) {
			checkLine(true);
			fDirectoryStack.removeAllElements();
			fBaseDirectory = null;
			if (outputStream != null)
				outputStream.close();
		}
	}

	/**
	 * @see java.io.OutputStream#flush()
	 */
	@Override
	public void flush() throws IOException {
		if (outputStream != null)
			outputStream.flush();
	}

	/**
	 * @see java.io.OutputStream#write(int)
	 */
	@Override
	public synchronized void write(int b) throws IOException {
		currentLine.append((char) b);
		checkLine(false);
		if (outputStream != null)
			outputStream.write(b);
	}

	@Override
	public synchronized void write(byte[] b, int off, int len) throws IOException {
		if (b == null) {
			throw new NullPointerException();
		} else if (off != 0 || (len < 0) || (len > b.length)) {
			throw new IndexOutOfBoundsException();
		} else if (len == 0) {
			return;
		}
		currentLine.append(new String(b, 0, len));
		checkLine(false);
		if (outputStream != null)
			outputStream.write(b, off, len);
	}

	private void checkLine(boolean flush) {
		String buffer = currentLine.toString();
		int i = 0;
		while ((i = buffer.indexOf('\n')) != -1) {
			String line = buffer.substring(0, i).trim(); // get rid of any trailing \r
			processLine(line);
			previousLine = line;
			buffer = buffer.substring(i + 1); // skip the \n and advance
		}
		currentLine.setLength(0);
		if (flush) {
			if (buffer.length() > 0) {
				processLine(buffer);
				previousLine = buffer;
			}
		} else {
			currentLine.append(buffer);
		}
	}

	public boolean reportProblems() {
		boolean reset = false;
		if (nOpens == 0) {
			Iterator<ProblemMarkerInfo> iter = fErrors.iterator();
			while (iter.hasNext()) {
				ProblemMarkerInfo problemMarkerInfo = iter.next();
				if (problemMarkerInfo.severity == IMarkerGenerator.SEVERITY_ERROR_BUILD) {
					reset = true;
				}
				fMarkerGenerator.addMarker(problemMarkerInfo);
			}
			fErrors.clear();
		}
		return reset;
	}

	/**
	 * 
	 */
	public String getScratchBuffer() {
		return scratchBuffer.toString();
	}

	/**
	 * @param line
	 */
	public void appendToScratchBuffer(String line) {
		scratchBuffer.append(line);
	}

	/**
	 * 
	 */
	public void clearScratchBuffer() {
		scratchBuffer.setLength(0);
	}
	
	public boolean hasErrors() {
		return hasErrors;
	}
}

Back to the top