Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8961cab5be2dd8d61e39ce3037caefba59ddd48 (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
/*******************************************************************************
 * Copyright (c) 2010 Google, Inc 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:
 * 	  Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.sourcelookup;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ISourceRoot;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.sourcelookup.IMappingSourceContainer;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
import org.eclipse.osgi.util.NLS;

/**
 * A project where source folders are used for running the C/C++ compiler. This container
 * is intended to be used when debugging information produced by the C/C++ compiler contains paths
 * to the source and header files relative to the directory where the compiler is run from.
 * The assumption is that all files under a source folder are compiled relative to that folder.
 * 
 * Source elements returned from <code>findSourceElements(...)</code> are instances of
 * <code>LocalFileStorage</code>.
 * <p>
 * Clients may instantiate this class. 
 * </p>
 * @noextend This class is not intended to be subclassed by clients.
 */
public class SourceFoldersRelativePathSourceContainer extends CompositeSourceContainer
		implements IMappingSourceContainer {
	/**
	 * Unique identifier for the container type
	 * (value <code>org.eclipse.cdt.debug.core.containerType.sourceFoldersRelativePath</code>).
	 */
	public static final String TYPE_ID =
			CDebugCorePlugin.getUniqueIdentifier() + ".containerType.sourceFoldersRelativePath"; //$NON-NLS-1$

	private final IProject fProject;
	private boolean fSearchReferencedProjects;

	/**
	 * Constructs a source folder relative path source container.
	 * 
	 * @param project the project to search for source in. A {@code null} project indicates
	 * 		the the project from the launch configuration should be used.
	 * @param referenced whether referenced projects should be considered
	 */
	public SourceFoldersRelativePathSourceContainer(IProject project, boolean referenced) {
		fProject = project;
		fSearchReferencedProjects = referenced;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#isComposite()
	 */
	public boolean isComposite() {
		return true;
	}

	/**
	 * Returns whether referenced projects are considered.
	 * 
	 * @return whether referenced projects are considered
	 */
	public boolean isSearchReferencedProjects() {
		return fSearchReferencedProjects;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
	 */
	public String getName() {
		IProject project = getResolvedProject();
		return project == null ?
				InternalSourceLookupMessages.SourceFoldersRelativePathSourceContainer_0 :
				NLS.bind(InternalSourceLookupMessages.SourceFoldersRelativePathSourceContainer_1, project.getName());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
	 */
	public ISourceContainerType getType() {
		return getSourceContainerType(TYPE_ID);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj != null && obj instanceof SourceFoldersRelativePathSourceContainer) {
			SourceFoldersRelativePathSourceContainer loc = (SourceFoldersRelativePathSourceContainer) obj;
			return fProject == null ? loc.fProject == null : fProject.equals(loc.fProject);
		}	
		return false;
	}	
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return TYPE_ID.hashCode() * 31 + (fProject == null ? 0 : fProject.hashCode());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.sourcelookup.IMappingSourceContainer#getCompilationPath(java.lang.String)
	 */
	public IPath getCompilationPath(String sourceName) {
		IProject project = getResolvedProject();
		if (project == null)
			return null;
		ICProject cProject = CModelManager.getDefault().create(project);
		IPath path = new Path(sourceName);
		for (IFile file : ResourceLookup.findFilesForLocation(path)) {
			ISourceRoot root = cProject.findSourceRoot(file);
			if (root != null) {
				return path.makeRelativeTo(root.getResource().getLocation());
			}
		}
		return null;
	}

	public IProject getProject() {
		return fProject;
	}

	/**
	 * Returns the project associated with this source container either directly or through
	 * the current launch configuration.
	 * @return an IProject instance or {@code null}.
	 */
	private IProject getResolvedProject() {
		if (fProject != null)
			return fProject;
		ISourceLookupDirector director = getDirector();
		if (director != null) {
			return SourceUtils.getLaunchConfigurationProject(director);
		}
		return null;
	}

	@Override
	protected ISourceContainer[] createSourceContainers() throws CoreException {
		IProject project = getResolvedProject();
		if (project != null && project.isOpen()) {
			if (isSearchReferencedProjects()) {
				IProject[] projects = SourceUtils.getAllReferencedProjects(project);
				ISourceContainer[] folders = createCompilationDirectoryContainers(project);
				List<ISourceContainer> containers = new ArrayList<ISourceContainer>(folders.length + projects.length);
				for (ISourceContainer folder : folders) {
					containers.add(folder);
				}
				for (IProject ref : projects) {
					if (ref.exists() && ref.isOpen()) {
						SourceFoldersRelativePathSourceContainer container =
								new SourceFoldersRelativePathSourceContainer(ref, false);
						container.init(getDirector());
						containers.add(container);
					}
				}
				return containers.toArray(new ISourceContainer[containers.size()]);
			} 
			return createCompilationDirectoryContainers(project);
		}
		return new ISourceContainer[0];
	}

	private ISourceContainer[] createCompilationDirectoryContainers(IProject project) throws CoreException {
		ICProject cProject = CModelManager.getDefault().create(project);
		ISourceRoot[] roots = cProject.getAllSourceRoots();
		List<ISourceContainer> list = new ArrayList<ISourceContainer>(roots.length);
		for (ISourceRoot root : roots) {
			IContainer folder = (IContainer) root.getResource();
			ISourceContainer container = new CompilationDirectorySourceContainer(folder.getLocation(), false);
			container.init(getDirector());
			list.add(container);
		}
		return list.toArray(new ISourceContainer[list.size()]);
	}
}

Back to the top