Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09f9d25d19157e39871b05ca421dd45f9d2afe15 (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
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat Inc - initial API
 *******************************************************************************/
package org.eclipse.cdt.core.errorparsers;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.ProblemMarkerInfo;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.internal.core.ProblemMarkerFilterManager;
import org.eclipse.cdt.internal.errorparsers.FixitManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

/**
 * @since 6.3
 */
public class FixitErrorParser extends RegexErrorParser {
	
	private final static Pattern fixit = Pattern.compile("fix-it:\"(.*)\":\\{(.*-.*)\\}:\"(.*)\""); //$NON-NLS-1$

	public FixitErrorParser (String id, String name) {
		super(id, name);
	}
	
	public FixitErrorParser () {
		super();
	}
	
	@Override
	public boolean processLine(String line, ErrorParserManager epManager) {
		Matcher m = fixit.matcher(line);
		if (m.matches()) {
			IProject project = null;
			IFile f = epManager.findFileName(m.group(1));
			if (f != null) {
				project = f.getProject();
				try {
					ProblemMarkerInfo info = ProblemMarkerFilterManager.getInstance().getLastProblemMarker(f);
					String externalLocation = null;
					if (info.externalPath != null && !info.externalPath.isEmpty()) {
						externalLocation = info.externalPath.toOSString();
					}

					// Try to find matching marker to tie to fix-it
					IMarker[] markers = f.findMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, true,
							IResource.DEPTH_ONE);
					for (IMarker marker : markers) {
						int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, -1);
						int sev = marker.getAttribute(IMarker.SEVERITY, -1);
						String msg = (String) marker.getAttribute(IMarker.MESSAGE);
						if (lineNumber == info.lineNumber
								&& sev == info.severity
								&& msg.equals(info.description)) {
							String extloc = (String) marker.getAttribute(ICModelMarker.C_MODEL_MARKER_EXTERNAL_LOCATION);
							if (extloc == null || extloc.equals(externalLocation)) {
								if (project == null || project.equals(info.file.getProject())) {
									FixitManager.getInstance().addMarker(marker, m.group(2), m.group(3));
									return true;
								}
								String source = (String) marker.getAttribute(IMarker.SOURCE_ID);
								if (project.getName().equals(source)) {
									FixitManager.getInstance().addMarker(marker, m.group(2), m.group(3));
									return true;
								}
							}
						}
					}
				} catch (CoreException | NumberFormatException e) {
					CCorePlugin.log(e);
				}
				return true;
			}
		}
		return super.processLine(line, epManager);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() throws CloneNotSupportedException {
		FixitErrorParser that = new FixitErrorParser(getId(), getName());
		for (RegexErrorPattern pattern : getPatterns()) {
			that.addPattern((RegexErrorPattern)pattern.clone());
		}
		return that;
	}
}

Back to the top