Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1215c53cf66f98eda2b8b889fa4ae2e6f4327516 (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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*******************************************************************************
 * Copyright (c) 2004, 2014 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.sourcelookup;

import java.io.IOException;
import java.io.StringReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.model.ICStackFrame;
import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocation;
import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator;
import org.eclipse.cdt.debug.core.sourcelookup.IProjectSourceLocation;
import org.eclipse.cdt.debug.core.sourcelookup.SourceLookupFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IPersistableSourceLocator;
import org.eclipse.debug.core.model.IStackFrame;
import org.osgi.framework.Bundle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Default source locator.
 */
public class CSourceLocator implements ICSourceLocator, IPersistableSourceLocator, IResourceChangeListener {
	private static final String SOURCE_LOCATOR_NAME = "cSourceLocator"; //$NON-NLS-1$
	private static final String DISABLED_GENERIC_PROJECT_NAME = "disabledGenericProject"; //$NON-NLS-1$
	private static final String ADDITIONAL_SOURCE_LOCATION_NAME = "additionalSourceLocation"; //$NON-NLS-1$
	private static final String SOURCE_LOCATION_NAME = "cSourceLocation"; //$NON-NLS-1$
	private static final String ATTR_CLASS = "class"; //$NON-NLS-1$
	private static final String ATTR_MEMENTO = "memento"; //$NON-NLS-1$
	private static final String ATTR_PROJECT_NAME = "projectName"; //$NON-NLS-1$
	private static final String ATTR_DUPLICATE_FILES = "duplicateFiles"; //$NON-NLS-1$

	/**
	 * The project associated with this locator.
	 */
	private IProject fProject;

	/**
	 * The array of source locations associated with this locator.
	 */
	private ICSourceLocation[] fSourceLocations;

	/**
	 * The array of projects referenced by main project.
	 */
	private List<IProject> fReferencedProjects = new ArrayList<>(10);

	/**
	 * The flag specifies whether to search for all source elements, or just the first match.
	 */
	private boolean fDuplicateFiles;

	/**
	 * Constructor for CSourceLocator.
	 */
	public CSourceLocator(IProject project) {
		setProject(project);
		setReferencedProjects();
		setSourceLocations(getDefaultSourceLocations());
	}

	@Override
	public Object getSourceElement(IStackFrame stackFrame) {
		return getInput(stackFrame);
	}

	@Override
	public int getLineNumber(IStackFrame frame) {
		return (frame instanceof ICStackFrame) ? ((ICStackFrame) frame).getFrameLineNumber() : 0;
	}

	protected Object getInput(IStackFrame f) {
		if (f instanceof ICStackFrame) {
			ICStackFrame frame = (ICStackFrame) f;
			LinkedList<Object> list = new LinkedList<>();
			Object result = null;
			String fileName = frame.getFile();
			if (fileName != null && fileName.length() > 0) {
				ICSourceLocation[] locations = getSourceLocations();
				for (int i = 0; i < locations.length; ++i) {
					try {
						result = locations[i].findSourceElement(fileName);
					} catch (CoreException e) {
						// do nothing
					}
					if (result != null) {
						if (result instanceof List)
							list.addAll((List<?>) result);
						else
							list.add(result);
						if (!searchForDuplicateFiles())
							break;
					}
				}
			}
			return (list.size() > 0) ? ((list.size() == 1) ? list.getFirst() : list) : null;
		}
		return null;
	}

	@Override
	public boolean contains(IResource resource) {
		ICSourceLocation[] locations = getSourceLocations();
		for (int i = 0; i < locations.length; ++i) {
			if (resource instanceof IProject) {
				if (locations[i] instanceof CProjectSourceLocation
						&& ((CProjectSourceLocation) locations[i]).getProject().equals(resource)) {
					return true;
				}
			}
			if (resource instanceof IFile) {
				try {
					Object result = locations[i].findSourceElement(resource.getLocation().toOSString());
					if (result instanceof IFile && ((IFile) result).equals(resource))
						return true;
					if (result instanceof List && ((List<?>) result).contains(resource))
						return true;
				} catch (CoreException e) {
				}
			}
		}
		return false;
	}

	@Override
	public ICSourceLocation[] getSourceLocations() {
		return fSourceLocations;
	}

	@Override
	public void setSourceLocations(ICSourceLocation[] locations) {
		fSourceLocations = locations;
	}

	/**
	 * Returns a default collection of source locations for the given project. Default source locations consist of the given project and all of its referenced
	 * projects.
	 *
	 * @param project
	 *            a project
	 * @return a collection of source locations for all required projects
	 * @exception CoreException
	 */
	public static ICSourceLocation[] getDefaultSourceLocations(IProject project) {
		ArrayList<IProjectSourceLocation> list = new ArrayList<>();
		if (project != null && project.exists()) {
			list.add(SourceLookupFactory.createProjectSourceLocation(project));
			addReferencedSourceLocations(list, project);
		}
		return list.toArray(new ICSourceLocation[list.size()]);
	}

	private static void addReferencedSourceLocations(List<IProjectSourceLocation> list, IProject project) {
		if (project != null) {
			try {
				IProject[] projects = project.getReferencedProjects();
				for (int i = 0; i < projects.length; i++) {
					if (projects[i].exists() && !containsProject(list, projects[i])) {
						list.add(SourceLookupFactory.createProjectSourceLocation(projects[i]));
						addReferencedSourceLocations(list, projects[i]);
					}
				}
			} catch (CoreException e) {
				// do nothing
			}
		}
	}

	private static boolean containsProject(List<IProjectSourceLocation> list, IProject project) {
		Iterator<IProjectSourceLocation> it = list.iterator();
		while (it.hasNext()) {
			CProjectSourceLocation location = (CProjectSourceLocation) it.next();
			if (project.equals(location.getProject()))
				return true;
		}
		return false;
	}

	@Override
	public Object findSourceElement(String fileName) {
		Object result = null;
		if (fileName != null && fileName.length() > 0) {
			ICSourceLocation[] locations = getSourceLocations();
			for (int i = 0; i < locations.length; ++i) {
				try {
					result = locations[i].findSourceElement(fileName);
				} catch (CoreException e) {
					// do nothing
				}
				if (result != null)
					break;
			}
		}
		return result;
	}

	@Override
	public String getMemento() throws CoreException {
		Document document = null;
		Throwable ex = null;
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element node = document.createElement(SOURCE_LOCATOR_NAME);
			document.appendChild(node);
			ICSourceLocation[] locations = getSourceLocations();
			saveDisabledGenericSourceLocations(locations, document, node);
			saveAdditionalSourceLocations(locations, document, node);
			node.setAttribute(ATTR_DUPLICATE_FILES, String.valueOf(searchForDuplicateFiles()));
			return CDebugUtils.serializeDocument(document);
		} catch (ParserConfigurationException e) {
			ex = e;
		} catch (IOException e) {
			ex = e;
		} catch (TransformerException e) {
			ex = e;
		}
		abort(InternalSourceLookupMessages.CSourceLocator_0, ex);
		// execution will not reach here
		return null;
	}

	@Override
	public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
		setSourceLocations(getDefaultSourceLocations());
	}

	@Override
	public void initializeFromMemento(String memento) throws CoreException {
		Exception ex = null;
		try {
			Element root = null;
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			StringReader reader = new StringReader(memento);
			InputSource source = new InputSource(reader);
			root = parser.parse(source).getDocumentElement();
			if (!root.getNodeName().equalsIgnoreCase(SOURCE_LOCATOR_NAME)) {
				abort(InternalSourceLookupMessages.CSourceLocator_1, null);
			}
			List<ICSourceLocation> sourceLocations = new ArrayList<>();
			// Add locations based on referenced projects
			IProject project = getProject();
			if (project != null && project.exists() && project.isOpen())
				sourceLocations.addAll(Arrays.asList(getDefaultSourceLocations()));
			removeDisabledLocations(root, sourceLocations);
			addAdditionalLocations(root, sourceLocations);
			// To support old launch configuration
			addOldLocations(root, sourceLocations);
			setSourceLocations(sourceLocations.toArray(new ICSourceLocation[sourceLocations.size()]));
			setSearchForDuplicateFiles(Boolean.valueOf(root.getAttribute(ATTR_DUPLICATE_FILES)).booleanValue());
			return;
		} catch (ParserConfigurationException e) {
			ex = e;
		} catch (SAXException e) {
			ex = e;
		} catch (IOException e) {
			ex = e;
		}
		abort(InternalSourceLookupMessages.CSourceLocator_2, ex);
	}

	private void removeDisabledLocations(Element root, List<ICSourceLocation> sourceLocations) {
		NodeList list = root.getChildNodes();
		int length = list.getLength();
		HashSet<String> disabledProjects = new HashSet<>(length);
		for (int i = 0; i < length; ++i) {
			Node node = list.item(i);
			short type = node.getNodeType();
			if (type == Node.ELEMENT_NODE) {
				Element entry = (Element) node;
				if (entry.getNodeName().equalsIgnoreCase(DISABLED_GENERIC_PROJECT_NAME)) {
					String projectName = entry.getAttribute(ATTR_PROJECT_NAME);
					if (isEmpty(projectName)) {
						CDebugCorePlugin.log("Unable to restore C/C++ source locator - invalid format."); //$NON-NLS-1$
					}
					disabledProjects.add(projectName.trim());
				}
			}
		}
		Iterator<ICSourceLocation> it = sourceLocations.iterator();
		while (it.hasNext()) {
			ICSourceLocation location = it.next();
			if (location instanceof IProjectSourceLocation
					&& disabledProjects.contains(((IProjectSourceLocation) location).getProject().getName()))
				it.remove();
		}
	}

	private void addAdditionalLocations(Element root, List<ICSourceLocation> sourceLocations) throws CoreException {
		Bundle bundle = CDebugCorePlugin.getDefault().getBundle();
		MultiStatus status = new MultiStatus(CDebugCorePlugin.getUniqueIdentifier(), CDebugCorePlugin.INTERNAL_ERROR,
				InternalSourceLookupMessages.CSourceLocator_3, null);
		NodeList list = root.getChildNodes();
		int length = list.getLength();
		for (int i = 0; i < length; ++i) {
			Node node = list.item(i);
			short type = node.getNodeType();
			if (type == Node.ELEMENT_NODE) {
				Element entry = (Element) node;
				if (entry.getNodeName().equalsIgnoreCase(ADDITIONAL_SOURCE_LOCATION_NAME)) {
					String className = entry.getAttribute(ATTR_CLASS);
					String data = entry.getAttribute(ATTR_MEMENTO);
					if (isEmpty(className)) {
						CDebugCorePlugin.log("Unable to restore C/C++ source locator - invalid format."); //$NON-NLS-1$
						continue;
					}
					Class<?> clazz = null;
					try {
						clazz = bundle.loadClass(className);
					} catch (ClassNotFoundException e) {
						CDebugCorePlugin.log(MessageFormat
								.format("Unable to restore source location - class not found {0}", className)); //$NON-NLS-1$
						continue;
					}
					ICSourceLocation location = null;
					try {
						location = (ICSourceLocation) clazz.newInstance();
					} catch (IllegalAccessException e) {
						CDebugCorePlugin.log("Unable to restore source location."); //$NON-NLS-1$
						continue;
					} catch (InstantiationException e) {
						CDebugCorePlugin.log("Unable to restore source location."); //$NON-NLS-1$
						continue;
					}
					try {
						location.initializeFrom(data);
						sourceLocations.add(location);
					} catch (CoreException e) {
						status.addAll(e.getStatus());
					}
				}
			}
		}
		if (status.getSeverity() > IStatus.OK)
			throw new CoreException(status);
	}

	private void addOldLocations(Element root, List<ICSourceLocation> sourceLocations) throws CoreException {
		Bundle bundle = CDebugCorePlugin.getDefault().getBundle();
		NodeList list = root.getChildNodes();
		int length = list.getLength();
		for (int i = 0; i < length; ++i) {
			Node node = list.item(i);
			short type = node.getNodeType();
			if (type == Node.ELEMENT_NODE) {
				Element entry = (Element) node;
				if (entry.getNodeName().equalsIgnoreCase(SOURCE_LOCATION_NAME)) {
					String className = entry.getAttribute(ATTR_CLASS);
					String data = entry.getAttribute(ATTR_MEMENTO);
					if (isEmpty(className)) {
						CDebugCorePlugin.log("Unable to restore C/C++ source locator - invalid format."); //$NON-NLS-1$
						continue;
					}
					Class<?> clazz = null;
					try {
						clazz = bundle.loadClass(className);
					} catch (ClassNotFoundException e) {
						CDebugCorePlugin.log(MessageFormat.format(
								"Unable to restore source location - class not found {0}", new Object[] { className })); //$NON-NLS-1$
						continue;
					}
					ICSourceLocation location = null;
					try {
						location = (ICSourceLocation) clazz.newInstance();
					} catch (IllegalAccessException e) {
						CDebugCorePlugin.log("Unable to restore source location."); //$NON-NLS-1$
						continue;
					} catch (InstantiationException e) {
						CDebugCorePlugin.log("Unable to restore source location."); //$NON-NLS-1$
						continue;
					}
					location.initializeFrom(data);
					if (!sourceLocations.contains(location)) {
						if (location instanceof CProjectSourceLocation)
							((CProjectSourceLocation) location).setGenerated(
									isReferencedProject(((CProjectSourceLocation) location).getProject()));
						sourceLocations.add(location);
					}
				}
			}
		}
	}

	/**
	 * Throws an internal error exception
	 */
	private void abort(String message, Throwable e) throws CoreException {
		IStatus s = new Status(IStatus.ERROR, CDebugCorePlugin.getUniqueIdentifier(), CDebugCorePlugin.INTERNAL_ERROR,
				message, e);
		throw new CoreException(s);
	}

	private boolean isEmpty(String string) {
		return string == null || string.trim().length() == 0;
	}

	@Override
	public void resourceChanged(IResourceChangeEvent event) {
		if (event.getSource() instanceof IWorkspace && event.getDelta() != null) {
			IResourceDelta[] deltas = event.getDelta().getAffectedChildren();
			if (deltas != null) {
				ArrayList<IResource> list = new ArrayList<>(deltas.length);
				for (int i = 0; i < deltas.length; ++i)
					if (deltas[i].getResource() instanceof IProject)
						list.add(deltas[i].getResource());
				resetSourceLocations(list);
			}
		}
	}

	private void saveDisabledGenericSourceLocations(ICSourceLocation[] locations, Document doc, Element node) {
		IProject project = getProject();
		if (project != null && project.exists() && project.isOpen()) {
			List<IProject> list = CDebugUtils.getReferencedProjects(project);
			HashSet<String> names = new HashSet<>(list.size() + 1);
			names.add(project.getName());
			for (IProject proj : list) {
				names.add(proj.getName());
			}
			for (int i = 0; i < locations.length; ++i) {
				if (locations[i] instanceof IProjectSourceLocation
						&& ((IProjectSourceLocation) locations[i]).isGeneric())
					names.remove(((IProjectSourceLocation) locations[i]).getProject().getName());
			}
			for (String name : names) {
				Element child = doc.createElement(DISABLED_GENERIC_PROJECT_NAME);
				child.setAttribute(ATTR_PROJECT_NAME, name);
				node.appendChild(child);
			}
		}
	}

	private void saveAdditionalSourceLocations(ICSourceLocation[] locations, Document doc, Element node) {
		for (int i = 0; i < locations.length; i++) {
			if (locations[i] instanceof IProjectSourceLocation && ((IProjectSourceLocation) locations[i]).isGeneric())
				continue;
			Element child = doc.createElement(ADDITIONAL_SOURCE_LOCATION_NAME);
			child.setAttribute(ATTR_CLASS, locations[i].getClass().getName());
			try {
				child.setAttribute(ATTR_MEMENTO, locations[i].getMemento());
			} catch (CoreException e) {
				CDebugCorePlugin.log(e);
				continue;
			}
			node.appendChild(child);
		}
	}

	@Override
	public IProject getProject() {
		return fProject;
	}

	protected void setProject(IProject project) {
		fProject = project;
	}

	private boolean isReferencedProject(IProject ref) {
		if (getProject() != null) {
			try {
				return Arrays.asList(getProject().getReferencedProjects()).contains(ref);
			} catch (CoreException e) {
				CDebugCorePlugin.log(e);
			}
		}
		return false;
	}

	private void setReferencedProjects() {
		fReferencedProjects.clear();
		fReferencedProjects = CDebugUtils.getReferencedProjects(getProject());
	}

	protected ICSourceLocation[] getDefaultSourceLocations() {
		ArrayList<IProjectSourceLocation> list = new ArrayList<>(fReferencedProjects.size());
		if (getProject() != null && getProject().exists() && getProject().isOpen())
			list.add(SourceLookupFactory.createProjectSourceLocation(getProject()));
		for (IProject project : fReferencedProjects) {
			if (project != null && project.exists() && project.isOpen())
				list.add(SourceLookupFactory.createProjectSourceLocation(project));
		}
		return list.toArray(new ICSourceLocation[list.size()]);
	}

	private void resetSourceLocations(List<IResource> affectedProjects) {
		if (affectedProjects.size() != 0 && getProject() != null) {
			if (!getProject().exists() || !getProject().isOpen()) {
				removeGenericSourceLocations();
			} else {
				updateGenericSourceLocations(affectedProjects);
			}
		}
	}

	private void removeGenericSourceLocations() {
		fReferencedProjects.clear();
		ICSourceLocation[] locations = getSourceLocations();
		ArrayList<ICSourceLocation> newLocations = new ArrayList<>(locations.length);
		for (int i = 0; i < locations.length; ++i) {
			if (!(locations[i] instanceof IProjectSourceLocation)
					|| !((IProjectSourceLocation) locations[i]).isGeneric())
				newLocations.add(locations[i]);
		}
		setSourceLocations(newLocations.toArray(new ICSourceLocation[newLocations.size()]));
	}

	private void updateGenericSourceLocations(List<IResource> affectedProjects) {
		List<IProject> newRefs = CDebugUtils.getReferencedProjects(getProject());
		ICSourceLocation[] locations = getSourceLocations();
		ArrayList<ICSourceLocation> newLocations = new ArrayList<>(locations.length);
		for (int i = 0; i < locations.length; ++i) {
			if (!(locations[i] instanceof IProjectSourceLocation)
					|| !((IProjectSourceLocation) locations[i]).isGeneric()) {
				newLocations.add(locations[i]);
			} else {
				IProject project = ((IProjectSourceLocation) locations[i]).getProject();
				if (project.exists() && project.isOpen()) {
					if (newRefs.contains(project) || project.equals(getProject())) {
						newLocations.add(locations[i]);
						newRefs.remove(project);
					}
				}
			}
		}
		for (IProject project : newRefs) {
			if (!fReferencedProjects.contains(project))
				newLocations.add(SourceLookupFactory.createProjectSourceLocation(project));
		}
		fReferencedProjects = newRefs;
		setSourceLocations(newLocations.toArray(new ICSourceLocation[newLocations.size()]));
	}

	@Override
	public boolean searchForDuplicateFiles() {
		return fDuplicateFiles;
	}

	@Override
	public void setSearchForDuplicateFiles(boolean search) {
		fDuplicateFiles = search;
		ICSourceLocation[] locations = getSourceLocations();
		for (int i = 0; i < locations.length; ++i)
			locations[i].setSearchForDuplicateFiles(search);
	}
}

Back to the top