Skip to content

Commit

Permalink
Accommodate external binary object files
Browse files Browse the repository at this point in the history
- Adorn external binary object icons
- Present symbols under external binary object resources
- Label external binary objects with filename only
- Present absent external binary objects with grey label
- Sort external binary objects by filename only
  • Loading branch information
jld01 committed Feb 3, 2024
1 parent d5ec9d7 commit a1a9d93
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2009 QNX Software Systems and others.
* Copyright (c) 2000, 2024 QNX Software Systems and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -11,9 +11,12 @@
* Contributors:
* QNX Software Systems - Initial API and implementation
* Markus Schorn (Wind River Systems)
* John Dallaway - Support external binary files (#630)
*******************************************************************************/
package org.eclipse.cdt.core.model;

import org.eclipse.core.runtime.IPath;

/**
* Represents a Binary file, for example an ELF executable.
* An ELF parser will inspect the binary.
Expand Down Expand Up @@ -57,4 +60,11 @@ public interface IBinary extends ICElement, IParent, IOpenable {
public boolean showInBinaryContainer();
//public IAddressFactory getAddressFactory();

/**
* Returns the absolute path of the location of this binary. May be {@code null},
* in case the location does not exist.
* @return an absolute path to the location, or {@code null}
* @since 8.4
*/
public IPath getLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.util.MementoTokenizer;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
Expand Down Expand Up @@ -94,12 +95,12 @@ public boolean computeChildren(OpenableInfo info, IResource res) {
}
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(objPath);
if (file == null) { // if object path is external to the workspace
// fallback to legacy behaviour
// TODO: support external paths in Binary class as we do in TranslationUnit
objPath = ar.getPath().append(objPath.lastSegment());
Binary binary = new Binary(this, URIUtil.toURI(objPath), obj);
info.addChild(binary);
} else {
Binary binary = new Binary(this, objPath, obj);
info.addChild(binary);
}
Binary binary = new Binary(this, objPath, obj);
info.addChild(binary);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* Anton Leherbauer (Wind River Systems)
* John Dallaway - Adapt for IBinaryFile (#413)
* John Dallaway - Support source file lookup from relative path (#652)
* John Dallaway - Add initial support for external paths (#630)
*******************************************************************************/
package org.eclipse.cdt.internal.core.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -43,10 +45,15 @@
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
import org.eclipse.cdt.internal.core.util.MementoTokenizer;
import org.eclipse.cdt.utils.UNCPathConverter;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
Expand All @@ -66,6 +73,7 @@ public class Binary extends Openable implements IBinary {

private long fLastModification;

private URI location;
private IBinaryObject binaryObject;
private boolean showInBinaryContainer;

Expand All @@ -75,6 +83,13 @@ public Binary(ICElement parent, IFile file, IBinaryObject bin) {
showInBinaryContainer = determineShowInBinaryContainer(bin);
}

public Binary(ICElement parent, URI uri, IBinaryObject bin) {
super(parent, (IResource) null, uri.toString(), ICElement.C_BINARY);
location = uri;
binaryObject = bin;
showInBinaryContainer = determineShowInBinaryContainer(bin);
}

private boolean determineShowInBinaryContainer(IBinaryObject bin) {
BinaryFilePresentation presentation = bin.getAdapter(BinaryFilePresentation.class);
if (presentation != null) {
Expand Down Expand Up @@ -284,7 +299,7 @@ boolean computeChildren(OpenableInfo info, IResource res) throws CModelException
// information. If not, fall back on information from the binary parser.
boolean showSourceFiles = Platform.getPreferencesService().getBoolean(CCorePlugin.PLUGIN_ID,
CCorePreferenceConstants.SHOW_SOURCE_FILES_IN_BINARIES, false, null);
if (!showSourceFiles || !addSourceFiles(info, res, obj, hash)) {
if (!showSourceFiles || (res == null) || !addSourceFiles(info, res, obj, hash)) {
ISymbol[] symbols = obj.getSymbols();
for (ISymbol symbol : symbols) {
switch (symbol.getType()) {
Expand Down Expand Up @@ -528,7 +543,63 @@ public boolean exists() {
IResource res = getResource();
if (res != null)
return res.exists();
return super.exists();
if (location != null) {
try {
IFileStore fileStore = EFS.getStore(location);
IFileInfo info = fileStore.fetchInfo();
return info.exists();
} catch (CoreException e) {
CCorePlugin.log(e);
}

}
return false;
}

@Override
public IPath getLocation() {
if (location == null) {
IFile file = getFile();
if (file != null) {
return file.getLocation();
} else {
return null;
}
}
return UNCPathConverter.toPath(location);
}

@Override
public URI getLocationURI() {
if (location == null) {
IFile file = getFile();
if (file != null) {
location = file.getLocationURI();
} else {
return null;
}
}
return location;
}

@Override
public IPath getPath() {
if (getFile() != null) {
return super.getPath();
}
IPath path = getLocation();
if (path != null) {
return path;
}
return super.getPath();
}

public IFile getFile() {
IResource res = super.getResource();
if (res instanceof IFile) {
return (IFile) res;
}
return null;
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion core/org.eclipse.cdt.ui/plugin.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2003, 2023 IBM Corporation, QNX Software Systems, and others.
# Copyright (c) 2003, 2024 IBM Corporation, QNX Software Systems, and others.
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -656,6 +656,8 @@ ShiftRightAction.label= &Shift Right
ShiftLeftAction.label= S&hift Left

# Decorators
binaryFileDecorator.label = C/C++ Binary Files
binaryFileDecorator.description = Decorates C/C++ executable, object, core and shared library files.
indexedFilesDecorator.label = C/C++ Indexed Files
indexedFilesDecorator.description = Decorates files indexed by C/C++ Indexer.
translationUnitDecorator.label = C/C++ Translation Units
Expand Down
11 changes: 11 additions & 0 deletions core/org.eclipse.cdt.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4445,6 +4445,17 @@
<objectClass name="org.eclipse.cdt.core.model.ITranslationUnit"/>
</enablement>
</decorator>
<decorator
class="org.eclipse.cdt.internal.ui.viewsupport.BinaryFileDecorator"
id="org.eclipse.cdt.ui.binaryFile"
label="%binaryFileDecorator.label"
lightweight="true"
state="true">
<description>%binaryFileDecorator.description</description>
<enablement>
<objectClass name="org.eclipse.cdt.core.model.IBinary"/>
</enablement>
</decorator>
<decorator
adaptable="true"
class="org.eclipse.cdt.internal.ui.viewsupport.ExcludedFileDecorator"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2011 IBM Corporation and others.
* Copyright (c) 2005, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -13,6 +13,7 @@
* QNX Software System
* Anton Leherbauer (Wind River Systems)
* Sergey Prigogin (Google)
* John Dallaway - process external binaries as C elements (#630)
*******************************************************************************/
package org.eclipse.cdt.internal.ui;

Expand Down Expand Up @@ -76,11 +77,13 @@ public Object getAdapter(Object element, Class key) {
}

private IPropertySource getPropertySource(ICElement celement) {
if (celement instanceof IBinary) {
return new BinaryPropertySource((IBinary) celement);
}
IResource res = celement.getResource();
if (res != null) {
if (celement instanceof IBinary) {
// IBinary objects must have an IResource at present
// TODO: support external binaries as for external translation units
return new BinaryPropertySource((IBinary) celement);
}
if (res instanceof IFile) {
return new FilePropertySource((IFile) res);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2023 Wind River Systems, Inc. and others.
* Copyright (c) 2006, 2024 Wind River Systems, Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -12,6 +12,7 @@
* Anton Leherbauer (Wind River Systems) - initial API and implementation
* Ed Swartz (Nokia)
* John Dallaway - do not handle absent external translation units (#563)
* John Dallaway - do not handle external binaries (#630)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.navigator;

Expand All @@ -21,6 +22,7 @@
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand Down Expand Up @@ -76,14 +78,27 @@ protected boolean updateSelection(IStructuredSelection selection) {
if (!(element instanceof ICElement) && element instanceof IAdaptable) {
element = ((IAdaptable) element).getAdapter(ICElement.class);
}
if (element instanceof ICElement && (element instanceof ISourceReference || element instanceof IBinary)) {
// do not handle absent external translation units
if (!(element instanceof ITranslationUnit tu) || (null != tu.getResource()) || tu.exists()) {
fOpenElement = (ICElement) element;
}
if ((element instanceof ICElement cElement) && canOpenCElement(cElement)) {
fOpenElement = (ICElement) element;
}
}
return fOpenElement != null || super.updateSelection(selection);
}

private boolean canOpenCElement(ICElement element) {
if (element instanceof ISourceReference sourceReference) {
if (null == sourceReference.getTranslationUnit()) {
return false; // no associated translation unit
}
// do not handle absent external translation units
return !(element instanceof ITranslationUnit tu) || (null != tu.getResource()) || tu.exists();
}
if (element instanceof IBinary) {
// do not handle external binaries
IResource resource = element.getResource();
return (null != resource) && resource.exists();
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2024 John Dallaway 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:
* John Dallaway - initial implementation (#630)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;

import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;
import org.eclipse.swt.graphics.Color;

/**
* Decorates binary files including executable, object, core and library files
*/
public final class BinaryFileDecorator extends BaseLabelProvider implements ILightweightLabelDecorator {

@Override
public boolean isLabelProperty(Object element, String property) {
return false;
}

@Override
public void decorate(Object element, IDecoration decoration) {
// if a binary file that is not present locally
if (element instanceof IBinary binary && !binary.exists()) {
// decorate label to indicate file is absent
Color color = JFaceResources.getColorRegistry().get(JFacePreferences.QUALIFIER_COLOR);
decoration.setForegroundColor(color);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2023 IBM Corporation and others.
* Copyright (c) 2005, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -13,7 +13,7 @@
* QNX Software System
* Markus Schorn (Wind River Systems)
* Anton Leherbauer (Wind River Systems)
* John Dallaway - use external file adornment (#563)
* John Dallaway - use external file adornment (#563, #630)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;

Expand Down Expand Up @@ -602,6 +602,9 @@ private int computeCAdornmentFlags(ICElement element, int renderFlags) {
}
}
}
if ((element instanceof IBinary) && (null == element.getResource())) {
flags |= CElementImageDescriptor.EXTERNAL_FILE;
}
} catch (CModelException e) {
}
}
Expand Down
Loading

0 comments on commit a1a9d93

Please sign in to comment.