Search in sources :

Example 11 with ICElement

use of org.eclipse.cdt.core.model.ICElement in project linuxtools by eclipse.

the class RemoteProxyCMainTab method handleSearchButtonSelected.

/**
 * Show a dialog that lists all main types
 */
@Override
protected void handleSearchButtonSelected() {
    if (getCProject() == null) {
        MessageDialog.openInformation(getShell(), LaunchMessages.CMainTab_Project_required, LaunchMessages.CMainTab_Enter_project_before_searching_for_program);
        return;
    }
    ILabelProvider programLabelProvider = new CElementLabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof IBinary) {
                IBinary bin = (IBinary) element;
                StringBuffer name = new StringBuffer();
                name.append(bin.getPath().lastSegment());
                return name.toString();
            }
            return super.getText(element);
        }

        @Override
        public Image getImage(Object element) {
            if (!(element instanceof ICElement)) {
                return super.getImage(element);
            }
            ICElement celement = (ICElement) element;
            if (celement.getElementType() == ICElement.C_BINARY) {
                IBinary belement = (IBinary) celement;
                if (belement.isExecutable()) {
                    return DebugUITools.getImage(IDebugUIConstants.IMG_ACT_RUN);
                }
            }
            return super.getImage(element);
        }
    };
    ILabelProvider qualifierLabelProvider = new CElementLabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof IBinary) {
                IBinary bin = (IBinary) element;
                StringBuffer name = new StringBuffer();
                name.append(bin.getCPU() + // $NON-NLS-1$ //$NON-NLS-2$
                (bin.isLittleEndian() ? "le" : "be"));
                // $NON-NLS-1$
                name.append(" - ");
                name.append(bin.getPath().toString());
                return name.toString();
            }
            return super.getText(element);
        }
    };
    TwoPaneElementSelector dialog = new TwoPaneElementSelector(getShell(), programLabelProvider, qualifierLabelProvider);
    dialog.setElements(getBinaryFiles(getCProject()));
    dialog.setMessage(LaunchMessages.CMainTab_Choose_program_to_run);
    dialog.setTitle(LaunchMessages.CMainTab_Program_Selection);
    dialog.setUpperListLabel(LaunchMessages.Launch_common_BinariesColon);
    dialog.setLowerListLabel(LaunchMessages.Launch_common_QualifierColon);
    dialog.setMultipleSelection(false);
    // dialog.set
    if (dialog.open() == Window.OK) {
        IBinary binary = (IBinary) dialog.getFirstResult();
        fProgText.setText(binary.getResource().getProjectRelativePath().toString());
    }
}
Also used : CElementLabelProvider(org.eclipse.cdt.ui.CElementLabelProvider) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) IBinary(org.eclipse.cdt.core.model.IBinary) ICElement(org.eclipse.cdt.core.model.ICElement) TwoPaneElementSelector(org.eclipse.ui.dialogs.TwoPaneElementSelector)

Example 12 with ICElement

use of org.eclipse.cdt.core.model.ICElement in project arduino-eclipse-plugin by Sloeber.

the class PdePreprocessor method extendHeaderForFile.

private static String extendHeaderForFile(String header, IIndex index, ITranslationUnit tu) throws CoreException {
    String localHeader = header;
    // Locate All lines that are extern "C"
    HashMap<Integer, Integer> externCLines = new HashMap<>();
    IASTTranslationUnit astTuTest = tu.getAST(index, 0);
    IASTDeclaration[] topDeclaratons = astTuTest.getDeclarations();
    for (IASTDeclaration curTopDeclaration : topDeclaratons) {
        ICPPASTLinkageSpecification test = curTopDeclaration instanceof ICPPASTLinkageSpecification ? (ICPPASTLinkageSpecification) curTopDeclaration : null;
        if (test != null) {
            if (test.getLiteral().equals("\"C\"")) {
                Path curFile = new Path(curTopDeclaration.getContainingFilename());
                if (curFile.equals(tu.getFile().getLocation())) {
                    int startLine = test.getFileLocation().getStartingLineNumber();
                    int endLine = test.getFileLocation().getEndingLineNumber();
                    for (int curline = startLine; curline <= endLine; curline++) {
                        externCLines.put(new Integer(curline), null);
                    }
                }
            }
        }
    }
    // find the last line containing a include
    IInclude[] includes = tu.getIncludes();
    int lastHeaderLine = 0;
    for (IInclude include : includes) {
        int curHeaderLine = include.getSourceRange().getEndLine();
        lastHeaderLine = Math.max(lastHeaderLine, curHeaderLine);
    }
    // parse line by line until all includes have been parsed
    for (int curline = 1; curline <= lastHeaderLine; curline++) {
        ICElement curElement = tu.getElementAtLine(curline);
        if (curElement != null) {
            switch(curElement.getElementType()) {
                case ICElement.C_MACRO:
                    IMacro curMacro = (IMacro) curElement;
                    if (curMacro.isActive()) {
                        localHeader += curMacro.getSource() + NEWLINE;
                    }
                    break;
                case ICElement.C_VARIABLE:
                    IVariable curVardeclar = (IVariable) curElement;
                    if (curVardeclar.isActive()) {
                        String fullTypeName = curVardeclar.getTypeName();
                        // ignore double arrays
                        if (fullTypeName.indexOf('[') == fullTypeName.lastIndexOf('[')) {
                            String typeName = fullTypeName.replace('[', ' ').replace(']', ' ').trim();
                            String typeExtensions = fullTypeName.replace(typeName, "");
                            localHeader += "extern " + typeName + " " + curVardeclar.getElementName() + typeExtensions + ";" + NEWLINE;
                        }
                    }
                    break;
                case ICElement.C_INCLUDE:
                    IInclude curInclude = (IInclude) curElement;
                    int curHeaderLine = curInclude.getSourceRange().getStartLine();
                    if (curInclude.isActive()) {
                        if (externCLines.containsKey(new Integer(curHeaderLine))) {
                            localHeader += "extern \"C\" {" + NEWLINE;
                            localHeader += curInclude.getSource() + NEWLINE;
                            localHeader += "}" + NEWLINE;
                        } else {
                            localHeader += curInclude.getSource();
                            localHeader += NEWLINE;
                        }
                    }
                    break;
            }
        }
    }
    return localHeader;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) HashMap(java.util.HashMap) IASTDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) IVariable(org.eclipse.cdt.core.model.IVariable) IInclude(org.eclipse.cdt.core.model.IInclude) ICPPASTLinkageSpecification(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification) ICElement(org.eclipse.cdt.core.model.ICElement) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IMacro(org.eclipse.cdt.core.model.IMacro)

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