Search in sources :

Example 6 with ICElement

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;
}
Also used : ICContainer(org.eclipse.cdt.core.model.ICContainer) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) ICElement(org.eclipse.cdt.core.model.ICElement) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit)

Example 7 with ICElement

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();
}
Also used : WorkbenchLabelProvider(org.eclipse.ui.model.WorkbenchLabelProvider) ICContainer(org.eclipse.cdt.core.model.ICContainer) Button(org.eclipse.swt.widgets.Button) CModelException(org.eclipse.cdt.core.model.CModelException) ArrayList(java.util.ArrayList) ICElement(org.eclipse.cdt.core.model.ICElement)

Example 8 with ICElement

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);
}
Also used : ICElement(org.eclipse.cdt.core.model.ICElement) IEditorInput(org.eclipse.ui.IEditorInput)

Example 9 with ICElement

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 "";
}
Also used : IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) ScannerInfo(org.eclipse.cdt.core.parser.ScannerInfo) IncludeFileContentProvider(org.eclipse.cdt.core.parser.IncludeFileContentProvider) IWorkingCopy(org.eclipse.cdt.core.model.IWorkingCopy) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator) InputStream(java.io.InputStream) IASTNode(org.eclipse.cdt.core.dom.ast.IASTNode) IOException(java.io.IOException) ICElement(org.eclipse.cdt.core.model.ICElement) IStorage(org.eclipse.core.resources.IStorage) IASTFunctionDefinition(org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition) FileContent(org.eclipse.cdt.core.parser.FileContent) ILanguage(org.eclipse.cdt.core.model.ILanguage) IASTNodeSelector(org.eclipse.cdt.core.dom.ast.IASTNodeSelector) CoreException(org.eclipse.core.runtime.CoreException) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) IWorkingCopyManager(org.eclipse.cdt.ui.IWorkingCopyManager)

Example 10 with ICElement

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);
    }
}
Also used : ICElement(org.eclipse.cdt.core.model.ICElement)

Aggregations

ICElement (org.eclipse.cdt.core.model.ICElement)12 ArrayList (java.util.ArrayList)4 ICContainer (org.eclipse.cdt.core.model.ICContainer)3 CoreException (org.eclipse.core.runtime.CoreException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)2 CModelException (org.eclipse.cdt.core.model.CModelException)2 IBinary (org.eclipse.cdt.core.model.IBinary)2 IProject (org.eclipse.core.resources.IProject)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 File (java.io.File)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 List (java.util.List)1 Map (java.util.Map)1 CCorePlugin (org.eclipse.cdt.core.CCorePlugin)1 IASTDeclaration (org.eclipse.cdt.core.dom.ast.IASTDeclaration)1 IASTFunctionDeclarator (org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator)1