1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 06:32:10 +02:00

[299369] Introduce source container that resolves paths relative to the executable. Applied patch, with modifications.

This commit is contained in:
John Cortell 2010-01-13 00:06:26 +00:00
parent 7b781f5d9b
commit db501eff16
12 changed files with 285 additions and 17 deletions

View file

@ -308,9 +308,6 @@ public class Binary extends Openable implements IBinary {
String[] sourceFiles = symbolreader.getSourceFiles(); String[] sourceFiles = symbolreader.getSourceFiles();
if (sourceFiles != null && sourceFiles.length > 0) { if (sourceFiles != null && sourceFiles.length > 0) {
for (String filename : sourceFiles) { for (String filename : sourceFiles) {
if (filename.startsWith(".")) { //$NON-NLS-1$
filename = obj.getPath().removeLastSegments(1).append(filename).toOSString();
}
File file = new File(filename); File file = new File(filename);
try { try {
if (file.exists()) { if (file.exists()) {

View file

@ -43,3 +43,5 @@ containerName.directory=File System Directory
containerDescription.directory=A directory in the local file system. containerDescription.directory=A directory in the local file system.
containerName.absolutePath=Absolute File Path containerName.absolutePath=Absolute File Path
containerDescription.absolutePath=An absolute path to a file in the local file system. containerDescription.absolutePath=An absolute path to a file in the local file system.
containerName.programRelativePath=Program Relative File Path
containerDescription.programRelativePath=Program relative path to a file in the local file system.

View file

@ -297,6 +297,12 @@
description="%containerDescription.absolutePath" description="%containerDescription.absolutePath"
id="org.eclipse.cdt.debug.core.containerType.absolutePath" id="org.eclipse.cdt.debug.core.containerType.absolutePath"
name="%containerName.absolutePath"/> name="%containerName.absolutePath"/>
<sourceContainerType
class="org.eclipse.cdt.debug.internal.core.sourcelookup.ProgramRelativePathSourceContainerType"
description="%containerDescription.programRelativePath"
id="org.eclipse.cdt.debug.core.containerType.programRelativePath"
name="%containerName.programRelativePath">
</sourceContainerType>
</extension> </extension>
<extension <extension
@ -326,6 +332,8 @@
point="org.eclipse.cdt.debug.core.supportedSourceContainerTypes"> point="org.eclipse.cdt.debug.core.supportedSourceContainerTypes">
<sourceContainer <sourceContainer
id="org.eclipse.cdt.debug.core.containerType.absolutePath"/> id="org.eclipse.cdt.debug.core.containerType.absolutePath"/>
<sourceContainer
id="org.eclipse.cdt.debug.core.containerType.programRelativePath"/>
<sourceContainer <sourceContainer
id="org.eclipse.cdt.debug.core.containerType.mapping"/> id="org.eclipse.cdt.debug.core.containerType.mapping"/>
<sourceContainer <sourceContainer

View file

@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright (c) 2008 Freescale 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:
* Freescale - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.debug.core.sourcelookup;
import java.io.File;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
/**
* A source container that converts relative paths to absolute ones using the
* program (executable) location as the reference point. The conversion is
* successful only if such a file actually exists.
*
* @since 7.0
*/
public class ProgramRelativePathSourceContainer extends AbstractSourceContainer{
/**
* Unique identifier for the relative path source container type
* (value <code>org.eclipse.cdt.debug.core.containerType.programRelativePath</code>).
*/
public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.programRelativePath"; //$NON-NLS-1$
/**
* The program's path.
*/
private IPath fProgramPath = Path.EMPTY;
/**
* Default constructor.
*/
public ProgramRelativePathSourceContainer() {
}
/**
* Special constructor used when trying to locate a source file without a
* launch or launch configuration context, but when a Binary context is
* available. Normally, this class is instantiated with the default (no arg)
* constructor and such instances are added to the source locator of a
* launch configuration. In those cases, we can obtain the the program
* (executable) context from the launch configuration. But in cases were CDT
* needs to search for a source file and there is no
* launch/launch-configuration context, it can explicitly create an instance
* using this constructor and call our {@link #findSourceElements(String)}
* method.
*
* @param program
* the executable context. Calling this with null is equivalent
* to calling the default constructor.
*/
public ProgramRelativePathSourceContainer(IBinary program) {
if (program != null) {
fProgramPath = program.getPath();
}
}
/**
* If [sourceName] is a relative path, and applying it to the location of
* the program (executable) produces an absolute path that points to an
* actual file, then we return a LocalFileStorage for that file. Otherwise
* we return an empty array. We always return at most one element.
*
* @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
*/
public Object[] findSourceElements( String sourceName ) throws CoreException {
if (sourceName == null){
return new Object[0];
}
// check if source path is a relative path
IPath sourcePath = new Path(sourceName);
if (sourcePath.isAbsolute()){
return new Object[0];
}
// get program (executable) absolute path
IPath programPath = getProgramLocation();
if (programPath == Path.EMPTY){
return new Object[0];
}
// remove the name of the program from the program path
programPath = programPath.removeLastSegments(1);
// append the relative source path to the absolute location of the program
sourcePath = programPath.append(sourcePath);
// check if source file exists and is valid
File sourceFile = sourcePath.toFile();
if ( sourceFile.exists() && sourceFile.isFile() ) {
return new Object[] { new LocalFileStorage( sourceFile ) };
}
return new Object[0];
}
/* (non-Javadoc)
* @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getName()
*/
public String getName() {
return SourceLookupMessages.getString("ProgramRelativePathSourceContainer.0"); //$NON-NLS-1$
}
/* (non-Javadoc)
* @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
*/
public ISourceContainerType getType() {
return getSourceContainerType( TYPE_ID );
}
private synchronized IPath getProgramLocation() throws CoreException {
// compute fProgramPath only if doesn't exist already
if (fProgramPath.isEmpty()){
// get launch configuration
ISourceLookupDirector director = getDirector();
if (director == null) {
return fProgramPath; // return empty path
}
ILaunchConfiguration configuration = director.getLaunchConfiguration();
if (configuration == null) {
return fProgramPath; // return empty path
}
// get current project
String projectName = configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
ICProject project = CoreModel.getDefault().getCModel().getCProject(projectName);
if (project == null || !project.exists()) {
return fProgramPath; // return empty path
}
// get program name
String programName = configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, (String)null);
if (programName == null){
return fProgramPath; // return empty path
}
// get executable file
IFile exeFile = null;
try {
exeFile = project.getProject().getFile(new Path(programName));
}
catch (IllegalArgumentException e){
return fProgramPath; // return empty path
}
if (!exeFile.exists()){
return fProgramPath; // return empty path
}
// get program absolute path
fProgramPath = exeFile.getLocation();
}
// return program absolute path
return fProgramPath;
}
}

View file

@ -11,3 +11,4 @@
############################################################################### ###############################################################################
MappingSourceContainer.0=Source lookup error MappingSourceContainer.0=Source lookup error
AbsolutePathSourceContainer.0=Absolute File Path AbsolutePathSourceContainer.0=Absolute File Path
ProgramRelativePathSourceContainer.0=Program Relative File Path

View file

@ -19,6 +19,7 @@ import java.util.Set;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.model.ICBreakpoint; import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.sourcelookup.AbsolutePathSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.AbsolutePathSourceContainer;
import org.eclipse.cdt.debug.core.sourcelookup.ProgramRelativePathSourceContainer;
import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer;
import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
@ -152,6 +153,14 @@ public class CSourceLookupDirector extends AbstractSourceLookupDirector {
if ( container instanceof AbsolutePathSourceContainer ) { if ( container instanceof AbsolutePathSourceContainer ) {
return ( ((AbsolutePathSourceContainer)container).isValidAbsoluteFilePath( sourceName ) ); return ( ((AbsolutePathSourceContainer)container).isValidAbsoluteFilePath( sourceName ) );
} }
if ( container instanceof ProgramRelativePathSourceContainer ) {
try {
Object[] elements = ((ProgramRelativePathSourceContainer)container).findSourceElements(sourceName);
return elements.length > 0;
} catch (CoreException e) {
return false;
}
}
try { try {
ISourceContainer[] containers; ISourceContainer[] containers;
containers = container.getSourceContainers(); containers = container.getSourceContainers();

View file

@ -17,6 +17,7 @@ import java.util.List;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.sourcelookup.AbsolutePathSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.AbsolutePathSourceContainer;
import org.eclipse.cdt.debug.core.sourcelookup.ProgramRelativePathSourceContainer;
import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer; import org.eclipse.cdt.debug.core.sourcelookup.MappingSourceContainer;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
@ -43,9 +44,27 @@ public class CSourcePathComputerDelegate implements ISourcePathComputerDelegate
* @see org.eclipse.debug.core.sourcelookup.ISourcePathComputerDelegate#computeSourceContainers(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.core.runtime.IProgressMonitor) * @see org.eclipse.debug.core.sourcelookup.ISourcePathComputerDelegate#computeSourceContainers(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.core.runtime.IProgressMonitor)
*/ */
public ISourceContainer[] computeSourceContainers( ILaunchConfiguration configuration, IProgressMonitor monitor ) throws CoreException { public ISourceContainer[] computeSourceContainers( ILaunchConfiguration configuration, IProgressMonitor monitor ) throws CoreException {
// First, get all the the containers in the global preferences // First, get all the the containers in the global preferences (but add them last)
ISourceContainer[] common = CDebugCorePlugin.getDefault().getCommonSourceLookupDirector().getSourceContainers(); ISourceContainer[] common = CDebugCorePlugin.getDefault().getCommonSourceLookupDirector().getSourceContainers();
List<ISourceContainer> containers = new ArrayList<ISourceContainer>( common.length + 1 );
List<ISourceContainer> containers = new ArrayList<ISourceContainer>( common.length + 2 );
// Add a container that fetches files that are specified with an absolute path
containers.add(new AbsolutePathSourceContainer() );
// Add a container that fetches files that are specified with a program relative path
containers.add(new ProgramRelativePathSourceContainer());
// Add a container that looks in the project specified in the configuration
String projectName = configuration.getAttribute( ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null );
if (projectName != null && projectName.length() > 0) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( projectName );
if ( project.exists() ) {
containers.add(new ProjectSourceContainer( project, true ) );
}
}
// Finally, add the common (global) containers
for ( ISourceContainer sc : common ) { for ( ISourceContainer sc : common ) {
// If the container is a path-mapper, use a copy (why?) // If the container is a path-mapper, use a copy (why?)
if ( sc.getType().getId().equals( MappingSourceContainer.TYPE_ID ) ) if ( sc.getType().getId().equals( MappingSourceContainer.TYPE_ID ) )
@ -53,18 +72,6 @@ public class CSourcePathComputerDelegate implements ISourcePathComputerDelegate
containers.add( sc ); containers.add( sc );
} }
// Add a container that looks in the project specified in the configuration
String projectName = configuration.getAttribute( ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null );
if (projectName != null && projectName.length() > 0) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( projectName );
if ( project.exists() ) {
containers.add( 0, new ProjectSourceContainer( project, true ) );
}
}
// Add a container that fetches files that are specified with an absolute path
containers.add( 0, new AbsolutePathSourceContainer() );
return containers.toArray( new ISourceContainer[containers.size()] ); return containers.toArray( new ISourceContainer[containers.size()] );
} }
} }

View file

@ -31,3 +31,4 @@ MappingSourceContainerType.0=Source lookup: unable to restore map entry - expect
MappingSourceContainerType.1=Source lookup: unable to restore mapping - expecting mapping element. MappingSourceContainerType.1=Source lookup: unable to restore mapping - expecting mapping element.
MappingSourceContainerType.2=Source lookup: unable to restore mapping - invalid memento. MappingSourceContainerType.2=Source lookup: unable to restore mapping - invalid memento.
SourceUtils.0=Mapping SourceUtils.0=Mapping
ProgramRelativePathSourceContainerType.1=Source lookup: unable to restore program relative source container - invalid memento.

View file

@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2008 Freescale 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:
* Freescale - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.debug.internal.core.sourcelookup;
import org.eclipse.cdt.debug.core.sourcelookup.ProgramRelativePathSourceContainer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainerTypeDelegate;
public class ProgramRelativePathSourceContainerType extends AbstractSourceContainerTypeDelegate {
private final static String ELEMENT_NAME = "programRelativePath"; //$NON-NLS-1$
public ISourceContainer createSourceContainer(String memento) throws CoreException {
if ( ELEMENT_NAME.equals( memento ) ) {
return new ProgramRelativePathSourceContainer();
}
abort( InternalSourceLookupMessages.getString("ProgramRelativePathSourceContainerType.1"), null ); //$NON-NLS-1$
return null;
}
public String getMemento(ISourceContainer container) throws CoreException {
if (container instanceof ProgramRelativePathSourceContainer){
return ELEMENT_NAME;
}
else{
return ""; //$NON-NLS-1$
}
}
}

View file

@ -21,6 +21,7 @@ import org.eclipse.cdt.core.ISourceFinder;
import org.eclipse.cdt.core.model.IBinary; import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.sourcelookup.ProgramRelativePathSourceContainer;
import org.eclipse.cdt.debug.internal.core.sourcelookup.CSourceLookupDirector; import org.eclipse.cdt.debug.internal.core.sourcelookup.CSourceLookupDirector;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit; import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.filesystem.URIUtil;
@ -73,6 +74,14 @@ public class CSourceFinder implements ISourceFinder, ILaunchConfigurationListene
*/ */
private Map<ILaunchConfiguration, ISourceLocator> fConfigLocators = Collections.synchronizedMap(new HashMap<ILaunchConfiguration, ISourceLocator>()); private Map<ILaunchConfiguration, ISourceLocator> fConfigLocators = Collections.synchronizedMap(new HashMap<ILaunchConfiguration, ISourceLocator>());
/**
* We use this when we don't have an ILaunch or ILaunchConfiguration
* locator. A program relative container instance is automatically added to
* every CDT launch configuration. So when we lack a configuration context,
* we rely on this container to help us resolve relative paths.
*/
private ProgramRelativePathSourceContainer fRelativePathContainer;
/** /**
* Constructor. * Constructor.
* *
@ -84,6 +93,8 @@ public class CSourceFinder implements ISourceFinder, ILaunchConfigurationListene
assert(binary != null); assert(binary != null);
fBinary = binary; fBinary = binary;
fRelativePathContainer = new ProgramRelativePathSourceContainer(binary);
ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager(); ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
lmgr.addLaunchConfigurationListener(this); lmgr.addLaunchConfigurationListener(this);
lmgr.addLaunchListener(this); lmgr.addLaunchListener(this);
@ -145,6 +156,16 @@ public class CSourceFinder implements ISourceFinder, ILaunchConfigurationListene
if (fLaunchLocator != null) { if (fLaunchLocator != null) {
foundElement = fLaunchLocator.getSourceElement(compilationPath); foundElement = fLaunchLocator.getSourceElement(compilationPath);
} }
else {
// If there isn't a launch/config locator, we need to explicitly
// try to resolve relative paths...relative to the binary
// location.
Object[] elements = fRelativePathContainer.findSourceElements(compilationPath);
if (elements.length > 0) {
assert elements.length == 1; // relative path container should return at most one element
foundElement = elements[0];
}
}
// If not found, look in the global (common) locator // If not found, look in the global (common) locator
if (foundElement == null) { if (foundElement == null) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 581 B

View file

@ -1647,6 +1647,10 @@
containerTypeID="org.eclipse.cdt.debug.core.containerType.absolutePath" containerTypeID="org.eclipse.cdt.debug.core.containerType.absolutePath"
icon="icons/obj16/abspath_obj.gif" icon="icons/obj16/abspath_obj.gif"
id="org.eclipse.cdt.debug.ui.sourceContainerPresentation.absolutePath"/> id="org.eclipse.cdt.debug.ui.sourceContainerPresentation.absolutePath"/>
<sourceContainerPresentation
containerTypeID="org.eclipse.cdt.debug.core.containerType.programRelativePath"
icon="icons/obj16/program_rel_path_obj.gif"
id="org.eclipse.cdt.debug.ui.sourceContainerPresentation.programRelativePath"/>
</extension> </extension>
<extension <extension
point="org.eclipse.core.filebuffers.annotationModelCreation"> point="org.eclipse.core.filebuffers.annotationModelCreation">