Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa2f112797de5b97a0ef44ad76bdc74bb7dcc8de (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Alena Laskavaia
 * 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:
 *    Alena Laskavaia  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.core.model;

import org.eclipse.cdt.codan.core.CodanCorePlugin;
import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.model.AbstractProblemReporter;
import org.eclipse.cdt.codan.core.model.IChecker;
import org.eclipse.cdt.codan.core.model.ICheckersRegistry;
import org.eclipse.cdt.codan.core.model.ICodanProblemMarker;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemReporterPersistent;
import org.eclipse.cdt.codan.core.model.IProblemReporterSessionPersistent;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * Problem reported that created eclipse markers
 */
public class CodanMarkerProblemReporter extends AbstractProblemReporter implements IProblemReporterPersistent,
		IProblemReporterSessionPersistent {
	private IResource resource;
	private IChecker checker;
	private ArrayList<ICodanProblemMarker> toAdd = new ArrayList<ICodanProblemMarker>();

	/**
	 * Create instance, which can be use as factory for
	 * IProblemReporterSessionPersistent or
	 * as IProblemReporterPersistent.
	 */
	public CodanMarkerProblemReporter() {
		super();
	}

	/**
	 * @param resource
	 * @param checker
	 */
	public CodanMarkerProblemReporter(IResource resource, IChecker checker) {
		this.resource = resource;
		this.checker = checker;
	}

	public IResource getResource() {
		return resource;
	}

	public IChecker getChecker() {
		return checker;
	}

	@Override
	protected void reportProblem(ICodanProblemMarker codanProblemMarker) {
		if (checker == null) {
			createProblem(codanProblemMarker);
		} else {
			toAdd.add(codanProblemMarker);
		}
	}

	/**
	 * @param codanProblemMarker
	 */
	protected IMarker createProblem(ICodanProblemMarker codanProblemMarker) {
		try {
			return codanProblemMarker.createMarker();
		} catch (CoreException e) {
			CodanCorePlugin.log(e);
			return null;
		}
	}

	public void deleteProblems(IResource file) {
		try {
			file.deleteMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, IResource.DEPTH_ZERO);
		} catch (CoreException ce) {
			CodanCorePlugin.log(ce);
		}
	}

	public void deleteAllProblems() {
		try {
			ResourcesPlugin.getWorkspace().getRoot().deleteMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, IResource.DEPTH_INFINITE);
		} catch (CoreException e) {
			CodanCorePlugin.log(e);
		}
	}

	public void deleteProblems(final IResource file, final IChecker checker) {
		try {
			ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
				public void run(IProgressMonitor monitor) throws CoreException {
					Collection<IMarker> markers = findResourceMarkers(file, checker);
					for (Iterator<IMarker> iterator = markers.iterator(); iterator.hasNext();) {
						IMarker iMarker = iterator.next();
						iMarker.delete();
					}
				}
			}, null, IWorkspace.AVOID_UPDATE, null);
		} catch (CoreException e) {
			CodanCorePlugin.log(e);
		}
	}

	protected Collection<IMarker> findResourceMarkers(IResource resource, IChecker checker) throws CoreException {
		Collection<IMarker> res = new ArrayList<IMarker>();
		IMarker[] markers;
		if (resource.exists()) {
			markers = resource.findMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, IResource.DEPTH_INFINITE);
		} else {
			if (resource.getProject() == null || !resource.getProject().isAccessible())
				return res;
			// non resource markers attached to a project itself
			markers = resource.getProject().findMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, IResource.DEPTH_ZERO);
		}
		ICheckersRegistry reg = CodanRuntime.getInstance().getCheckersRegistry();
		Collection<IProblem> problems = reg.getRefProblems(checker);
		for (int i = 0; i < markers.length; i++) {
			IMarker m = markers[i];
			String id = m.getAttribute(ICodanProblemMarker.ID, ""); //$NON-NLS-1$
			for (Iterator<IProblem> iterator = problems.iterator(); iterator.hasNext();) {
				IProblem iProblem = iterator.next();
				if (iProblem.getId().equals(id)) {
					res.add(m);
				}
			}
		}
		return res;
	}

	/**
	 * @param resource
	 * @param checker
	 * @return session aware problem reporter
	 * @since 1.1
	 */
	public IProblemReporterSessionPersistent createReporter(IResource resource, IChecker checker) {
		return new CodanMarkerProblemReporter(resource, checker);
	}

	public void start() {
		if (checker == null)
			deleteProblems(false);
	}

	public void done() {
		if (checker != null) {
			if (toAdd.size() == 0)
				deleteProblems(false);
			else
				reconcileMarkers();
			toAdd.clear();
		}
	}

	protected void reconcileMarkers() {
		try {
			ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
				public void run(IProgressMonitor monitor) throws CoreException {
					Collection<IMarker> markers = findResourceMarkers(resource, checker);
					for (Iterator<IMarker> iterator = markers.iterator(); iterator.hasNext();) {
						IMarker m = iterator.next();
						ICodanProblemMarker cm = similarMarker(m);
						if (cm == null) {
							m.delete();
						} else {
							updateMarker(m, cm);
							toAdd.remove(cm);
						}
					}
					for (Iterator<ICodanProblemMarker> iterator = toAdd.iterator(); iterator.hasNext();) {
						ICodanProblemMarker cm = iterator.next();
						cm.createMarker();
					}
				}
			}, null, IWorkspace.AVOID_UPDATE, null);
		} catch (CoreException e) {
			CodanCorePlugin.log(e);
		}
	}

	/**
	 * @param m
	 * @param cm
	 */
	protected void updateMarker(IMarker m, ICodanProblemMarker cm) {
		IProblemLocation loc = cm.getLocation();
		try {
			if (m.getAttribute(IMarker.LINE_NUMBER, 0) != loc.getLineNumber())
				m.setAttribute(IMarker.LINE_NUMBER, loc.getLineNumber());
			if (m.getAttribute(IMarker.CHAR_START, 0) != loc.getStartingChar())
				m.setAttribute(IMarker.CHAR_START, loc.getStartingChar());
			if (m.getAttribute(IMarker.CHAR_END, 0) != loc.getEndingChar())
				m.setAttribute(IMarker.CHAR_END, loc.getEndingChar());
			int severity = cm.getProblem().getSeverity().intValue();
			if (m.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO) != severity)
				m.setAttribute(IMarker.SEVERITY, severity);
		} catch (CoreException e) {
			try {
				m.delete();
				cm.createMarker();
			} catch (CoreException e1) {
				CodanCorePlugin.log(e1);
			}
		}
	}

	/**
	 * @param m
	 * @return
	 */
	protected ICodanProblemMarker similarMarker(IMarker m) {
		ICodanProblemMarker mcm = CodanProblemMarker.createCodanProblemMarkerFromResourceMarker(m);
		ArrayList<ICodanProblemMarker> cand = new ArrayList<ICodanProblemMarker>();
		for (Iterator<ICodanProblemMarker> iterator = toAdd.iterator(); iterator.hasNext();) {
			ICodanProblemMarker cm = iterator.next();
			if (mcm.equals(cm))
				return cm;
			if (similarTo(mcm, cm)) {
				cand.add(cm);
			}
		}
		if (cand.size() == 1)
			return cand.get(0);
		return null;
	}

	/**
	 * @param mcm
	 * @param cm
	 * @return
	 */
	private boolean similarTo(ICodanProblemMarker mcm, ICodanProblemMarker other) {
		if (!mcm.getProblem().getId().equals(other.getProblem().getId()))
			return false;
		if (!Arrays.equals(mcm.getArgs(), other.getArgs()))
			return false;
		IProblemLocation loc1 = mcm.getLocation();
		IProblemLocation loc2 = other.getLocation();
		if (!loc1.getFile().equals(loc2.getFile()))
			return false;
		if (Math.abs(loc1.getLineNumber() - loc2.getLineNumber()) > 2)
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.cdt.codan.core.model.IProblemReporterSessionPersistent#
	 * deleteProblems(boolean)
	 */
	public void deleteProblems(boolean all) {
		if (all == false)
			deleteProblems(resource, checker);
		else
			throw new UnsupportedOperationException();
	}
}

Back to the top