Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0fa53302dcfe937b3fa24ed4bd3a522bed50c8a (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems 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
 *******************************************************************************/
package org.eclipse.cdt.build.core;

import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;

/**
 * This could be temporary. Provides a core parser for the TextConsole's
 * IPatternMatchListener.
 * 
 * TODO that framework doesn't work well for builds that need to use different
 * parsers at different times. Should consider taking that architecture and
 * making it work well for our needs.
 * 
 * @since 5.12
 */
public abstract class CConsoleParser {

	private final String pattern;
	private final int flags;
	private final String lineQualifier;

	private final Pattern errorPattern;

	public static final String LINK_OFFSET = "cdt.link.offset"; //$NON-NLS-1$
	public static final String LINK_LENGTH = "cdt.link.length"; //$NON-NLS-1$

	protected CConsoleParser(String pattern, int flags, String lineQualifier) {
		this.pattern = pattern;
		this.flags = flags;
		this.lineQualifier = lineQualifier;
		this.errorPattern = Pattern.compile(pattern);
	}

	protected CConsoleParser(String pattern) {
		this(pattern, 0, null);
	}

	/**
	 * Returns the pattern to be used for matching. The pattern is a string
	 * representing a regular expression.
	 * 
	 * @return the regular expression to be used for matching
	 */
	public String getPattern() {
		return pattern;
	}

	/**
	 * Returns the flags to use when compiling this pattern match listener's
	 * regular expression, as defined by by
	 * <code>Pattern.compile(String regex, int flags)</code>
	 * 
	 * @return the flags to use when compiling this pattern match listener's
	 *         regular expression
	 * @see java.util.regex.Pattern#compile(java.lang.String, int)
	 */
	public int getCompilerFlags() {
		return flags;
	}

	/**
	 * Returns a simple regular expression used to identify lines that may match
	 * this pattern matcher's complete pattern, or <code>null</code>. Use of
	 * this attribute can improve performance by disqualifying lines from the
	 * search. When a line is found containing a match for this expression, the
	 * line is searched from the beginning for this pattern matcher's complete
	 * pattern. Lines not containing this pattern are discarded.
	 * 
	 * @return a simple regular expression used to identify lines that may match
	 *         this pattern matcher's complete pattern, or <code>null</code>
	 */
	public String getLineQualifier() {
		return lineQualifier;
	}

	protected abstract String getFileName(Matcher matcher);

	protected abstract int getLineNumber(Matcher matcher);

	protected abstract String getMessage(Matcher matcher);

	protected abstract int getSeverity(Matcher matcher);

	protected abstract int getLinkOffset(Matcher matcher);

	protected abstract int getLinkLength(Matcher matcher);

	public IMarker generateMarker(Path buildDirectory, String text) {
		Matcher matcher = errorPattern.matcher(text);
		if (matcher.matches()) {
			String fileName = getFileName(matcher);

			Path filePath = buildDirectory.resolve(fileName);

			IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(filePath.toUri());
			if (files.length > 0) {
				IFile file = files[0];
				if (file.exists()) {
					try {
						IMarker marker = file.createMarker(ICModelMarker.C_MODEL_PROBLEM_MARKER);
						marker.setAttribute(IMarker.MESSAGE, getMessage(matcher));
						marker.setAttribute(IMarker.SEVERITY, getSeverity(matcher));
						marker.setAttribute(IMarker.LINE_NUMBER, getLineNumber(matcher));
						marker.setAttribute(IMarker.CHAR_START, -1);
						marker.setAttribute(IMarker.CHAR_END, -1);
						marker.setAttribute(LINK_OFFSET, getLinkOffset(matcher));
						marker.setAttribute(LINK_LENGTH, getLinkLength(matcher));
						return marker;
					} catch (CoreException e) {
						CCorePlugin.log(e);
						return null;
					}
				}
			}
		}
		return null;
	}

}

Back to the top