use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.
the class SystemTapLaunchShortcut method getFunctionsFromBinary.
/**
* Retrieves the names of all functions referenced by the binary. If
* searchForResource is true, this function will return all function names
* belonging to an element with name matching the String held by
* resourceToSearchFor. Otherwise it will create a dialog prompting the user
* to select from a list of files to profile, or select the only available
* file if only one file is available.
*
* @param bin
* @return
*/
protected String getFunctionsFromBinary(IBinary bin, String targetResource) {
// $NON-NLS-1$
String funcs = "";
if (bin == null) {
return funcs;
}
try {
ArrayList<ICContainer> list = new ArrayList<>();
TranslationUnitVisitor v = new TranslationUnitVisitor();
for (ICElement b : bin.getCProject().getChildrenOfType(ICElement.C_CCONTAINER)) {
ICContainer c = (ICContainer) b;
for (ITranslationUnit tu : c.getTranslationUnits()) {
if (searchForResource && tu.getElementName().contains(targetResource)) {
tu.accept(v);
funcs += v.getFunctions();
return funcs;
} else {
if (!list.contains(c)) {
list.add(c);
}
}
}
// Iterate down to all children, checking for more C_Containers
while (c.getChildrenOfType(ICElement.C_CCONTAINER).size() > 0) {
ICContainer e = null;
for (ICElement d : c.getChildrenOfType(ICElement.C_CCONTAINER)) {
e = (ICContainer) d;
for (ITranslationUnit tu : e.getTranslationUnits()) {
if (searchForResource && tu.getElementName().contains(targetResource)) {
tu.accept(v);
funcs += (v.getFunctions());
return funcs;
} else {
if (!list.contains(c)) {
list.add(c);
}
}
}
}
c = e;
}
}
int numberOfFiles = numberOfValidFiles(list.toArray());
if (numberOfFiles == 1) {
for (ICContainer c : list) {
for (ITranslationUnit e : c.getTranslationUnits()) {
if (validElement(e)) {
e.accept(v);
funcs += v.getFunctions();
}
}
}
} else {
Object[] unitList = chooseUnit(list, numberOfFiles);
if (unitList == null || unitList.length == 0) {
return null;
} else if (unitList.length == 1 && unitList[0].toString().equals(USER_SELECTED_ALL)) {
// $NON-NLS-1$
funcs = "*";
return funcs;
}
StringBuilder tmpFunc = new StringBuilder();
for (String item : getAllFunctions(bin.getCProject(), unitList)) {
tmpFunc.append(item);
// $NON-NLS-1$
tmpFunc.append(" ");
}
funcs = tmpFunc.toString();
}
return funcs;
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}
use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.
the class SystemTapLaunchShortcut method chooseUnit.
/**
* Creates a dialog that prompts the user to select from the given list of
* ICElements
*
* @param list
* : list of ICElements
* @return
*/
private Object[] chooseUnit(List<ICContainer> list, int numberOfValidFiles) {
ListTreeContentProvider prov = new ListTreeContentProvider();
RuledTreeSelectionDialog dialog = new RuledTreeSelectionDialog(getActiveWorkbenchShell(), new WorkbenchLabelProvider(), prov);
// $NON-NLS-1$
dialog.setTitle(Messages.getString("SystemTapLaunchShortcut.SelectFiles"));
// $NON-NLS-1$
dialog.setMessage(Messages.getString("SystemTapLaunchShortcut.SelectFilesMsg"));
dialog.setInput(list);
dialog.setHelpAvailable(false);
dialog.setStatusLineAboveButtons(false);
dialog.setEmptyListMessage(Messages.getString(// $NON-NLS-1$
"SystemTapLaunchShortcut.NoFiles"));
dialog.setContainerMode(true);
Object[] topLevel = prov.findElements(list);
dialog.setInitialSelections(topLevel);
dialog.setSize(cap(topLevel.length * 10, 30, 55), cap((int) (topLevel.length * 1.5), 3, 13));
dialog.create();
Button okButton = dialog.getOkButton();
Object[] result = null;
if (testMode) {
okButton.setSelection(true);
result = list.toArray();
ArrayList<Object> output = new ArrayList<>();
try {
for (Object obj : result) {
if (obj instanceof ICContainer) {
ICElement[] array = ((ICContainer) obj).getChildren();
for (ICElement c : array) {
if (!(validElement(c))) {
continue;
}
if (c.getElementName().contains(MAIN_FUNC_NAME) && !output.contains(c)) {
output.add(c);
}
}
}
}
if (output.size() >= numberOfValidFiles) {
output.clear();
output.add(USER_SELECTED_ALL);
}
} catch (CModelException e) {
e.printStackTrace();
}
result = output.toArray();
} else {
if (dialog.open() == Window.CANCEL) {
return null;
}
result = dialog.getResult();
}
if (result == null) {
return null;
}
ArrayList<Object> output = new ArrayList<>();
try {
for (Object obj : result) {
if (obj instanceof ICContainer) {
ICElement[] array = ((ICContainer) obj).getChildren();
for (ICElement c : array) {
if (!(validElement(c))) {
continue;
}
if (!output.contains(c)) {
output.add(c);
}
}
} else if ((obj instanceof ICElement) && validElement((ICElement) obj) && !output.contains(obj)) {
output.add(obj);
}
}
if (output.size() >= numberOfValidFiles) {
output.clear();
output.add(USER_SELECTED_ALL);
}
} catch (CModelException e) {
e.printStackTrace();
}
return output.toArray();
}
use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.
the class GcovAnnotationModel method findSourceCoverageForEditor.
private SourceFile findSourceCoverageForEditor() {
if (editor.isDirty()) {
return null;
}
final IEditorInput input = editor.getEditorInput();
if (input == null) {
return null;
}
ICElement element = CDTUITools.getEditorInputCElement(input);
if (element == null) {
return null;
}
return findSourceCoverageForElement(element);
}
use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.
the class CParser method parseCurrentFunction.
@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {
String currentElementName;
if (input instanceof IFileEditorInput) {
// Get the working copy and connect to input.
IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
manager.connect(input);
// Retrieve the C/C++ Element in question.
IWorkingCopy workingCopy = manager.getWorkingCopy(input);
ICElement method = workingCopy.getElementAtOffset(offset);
manager.disconnect(input);
// no element selected
if (method == null)
return "";
// Get the current element name, to test it.
currentElementName = method.getElementName();
// Element doesn't have a name. Can go no further.
if (currentElementName == null) {
// element doesn't have a name
return "";
}
// Get the Element Type to test.
int elementType = method.getElementType();
switch(elementType) {
case ICElement.C_FIELD:
case ICElement.C_METHOD:
case ICElement.C_FUNCTION:
break;
case ICElement.C_MODEL:
return "";
// So it's not a method, field, function, or model. Where are we?
default:
ICElement tmpMethodType;
if (((tmpMethodType = method.getAncestor(ICElement.C_FUNCTION)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_METHOD)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_CLASS)) == null)) {
return "";
} else {
// In a class, but not in a method. Return class name instead.
method = tmpMethodType;
currentElementName = method.getElementName();
}
}
// Build all ancestor classes.
// Append all ancestor class names to string
ICElement tmpParent = method.getParent();
while (tmpParent != null) {
ICElement tmpParentClass = tmpParent.getAncestor(ICElement.C_CLASS);
if (tmpParentClass != null) {
String tmpParentClassName = tmpParentClass.getElementName();
if (tmpParentClassName == null)
return currentElementName;
currentElementName = tmpParentClassName + "." + currentElementName;
} else
return currentElementName;
tmpParent = tmpParentClass.getParent();
}
return currentElementName;
} else if (input instanceof IStorageEditorInput) {
// Get the working copy and connect to input.
// don't follow inclusions
currentElementName = "";
IStorageEditorInput sei = (IStorageEditorInput) input;
// don't follow inclusions
IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getEmptyFilesProvider();
// empty scanner info
IScannerInfo scanInfo = new ScannerInfo();
IStorage ancestorStorage = sei.getStorage();
if (ancestorStorage == null)
return "";
InputStream stream = ancestorStorage.getContents();
byte[] buffer = new byte[100];
String data = "";
int read = 0;
try {
do {
read = stream.read(buffer);
if (read > 0) {
String tmp = new String(buffer, 0, read);
data = data.concat(tmp);
}
} while (read == 100);
stream.close();
} catch (IOException e) {
// do nothing
}
// $NON-NLS-1$
FileContent content = FileContent.create("<text>", data.toCharArray());
// determine the language
ILanguage language = GPPLanguage.getDefault();
try {
IASTTranslationUnit ast;
int options = 0;
ast = language.getASTTranslationUnit(content, scanInfo, contentProvider, null, options, ParserUtil.getParserLogService());
IASTNodeSelector n = ast.getNodeSelector(null);
IASTNode node = n.findFirstContainedNode(offset, 100);
while (node != null && !(node instanceof IASTTranslationUnit)) {
if (node instanceof IASTFunctionDefinition) {
IASTFunctionDefinition fd = (IASTFunctionDefinition) node;
IASTFunctionDeclarator d = fd.getDeclarator();
currentElementName = new String(d.getName().getSimpleID());
break;
}
node = node.getParent();
}
} catch (CoreException exc) {
currentElementName = "";
CUIPlugin.log(exc);
}
return currentElementName;
}
return "";
}
use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.
the class RemoteProxyCMainTab method setDefaults.
@Override
public void setDefaults(ILaunchConfigurationWorkingCopy config) {
// We set empty attributes for project & program so that when one config
// is
// compared to another, the existence of empty attributes doesn't cause
// an
// incorrect result (the performApply() method can result in empty
// values
// for these attributes being set on a config if there is nothing in the
// corresponding text boxes)
// plus getContext will use this to base context from if set.
config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_ID, EMPTY_STRING);
config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, EMPTY_STRING);
// Set the auto choose build configuration to true for new
// configurations.
// Existing configurations created before this setting was introduced
// will have this disabled.
config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_AUTO, true);
ICElement cElement = null;
cElement = getContext(config, getPlatform(config));
if (cElement != null) {
initializeCProject(cElement, config);
initializeProgramName(cElement, config);
} else {
// don't want to remember the interim value from before
config.setMappedResources(null);
}
if (wantsTerminalOption()) {
config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL, ICDTLaunchConfigurationConstants.USE_TERMINAL_DEFAULT);
}
}
Aggregations