From 1d6fff500af2a33169fbd9cc35b48b6b7f8a6351 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Wed, 13 Jan 2010 19:17:32 +0000 Subject: [PATCH] [299542] Source locators not honored when opening a file by double clicking a Binary child element --- .../cdt/internal/core/model/Binary.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java index d8ff2809ff9..2cfe32634d9 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java @@ -22,6 +22,7 @@ import java.util.Map; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePreferenceConstants; +import org.eclipse.cdt.core.ISourceFinder; import org.eclipse.cdt.core.ISymbolReader; import org.eclipse.cdt.core.IBinaryParser.IBinaryExecutable; import org.eclipse.cdt.core.IBinaryParser.IBinaryFile; @@ -308,29 +309,40 @@ public class Binary extends Openable implements IBinary { String[] sourceFiles = symbolreader.getSourceFiles(); if (sourceFiles != null && sourceFiles.length > 0) { for (String filename : sourceFiles) { - File file = new File(filename); - try { - if (file.exists()) { - filename = file.getCanonicalPath(); + + // Find the file locally + ISourceFinder srcFinder = (ISourceFinder) getAdapter(ISourceFinder.class); + if (srcFinder != null) { + String localPath = srcFinder.toLocalPath(filename); + if (localPath != null) { + filename = localPath; } - } catch (IOException e) { // Do nothing. } + // Be careful how you use this File object. If filename is a relative path, the resulting File + // object will apply the relative path to the working directory, which is not what we want. + // Stay away from methods that return or use the absolute path of the object. Note that + // File.isAbsolute() returns false when the object was constructed with a relative path. + File file = new File(filename); + // Create a translation unit for this file and add it as a child of the binary String id = CoreModel.getRegistedContentTypeId(getCProject().getProject(), file.getName()); if (id == null) { // Don't add files we can't get an ID for. continue; } + // See if this source file is already in the project. // We check this to determine if we should create a TranslationUnit or ExternalTranslationUnit IFile wkspFile = null; - IFile[] filesInWP = ResourceLookup.findFilesForLocation(new Path(filename)); - - for (IFile element : filesInWP) { - if (element.isAccessible()) { - wkspFile = element; - break; + if (file.isAbsolute()) { + IFile[] filesInWP = ResourceLookup.findFilesForLocation(new Path(filename)); + + for (IFile element : filesInWP) { + if (element.isAccessible()) { + wkspFile = element; + break; + } } } @@ -338,12 +350,17 @@ public class Binary extends Openable implements IBinary { if (wkspFile != null) tu = new TranslationUnit(this, wkspFile, id); else { - IPath path = Path.fromOSString(filename); - if (path.toFile().exists()) { - tu = new ExternalTranslationUnit(this, path, id); + // If we have an absolute path (for the host file system), then use an IPath to create the + // ExternalTranslationUnit, as that is the more accurate way to specify the file. If it's + // not, then use the path specification we got from the debug information. We want to + // avoid, e.g., converting a UNIX path to a Windows one when debugging a UNIX-built binary + // on Windows. The opportunity to remap source paths was taken above, when we called + // ISourceFinder. If a mapping didn't occur, we want to preserve whatever the debug + // information told us. See bugzilla 297781 + if (file.isAbsolute()) { + tu = new ExternalTranslationUnit(this, Path.fromOSString(filename), id); } else { - // filename may be a UNIX path; don't convert to c:\ on Windows. tu = new ExternalTranslationUnit(this, URIUtil.toURI(filename), id); } }