Search in sources :

Example 1 with IBinding

use of org.eclipse.cdt.core.dom.ast.IBinding in project linuxtools by eclipse.

the class LibHover method getFunctionInfo.

@Override
public IFunctionSummary getFunctionInfo(ICHelpInvocationContext context, ICHelpBook[] helpBooks, String name) {
    IFunctionSummary f;
    f = null;
    ITranslationUnit t = context.getTranslationUnit();
    String className = null;
    ICPPFunctionType methodType = null;
    if (t.isCXXLanguage()) {
        try {
            if (context instanceof IHoverHelpInvocationContext) {
                // We know the file offset of the member reference.
                IRegion region = ((IHoverHelpInvocationContext) context).getHoverRegion();
                // Now, let's find the declaration of the method.  We need to do this because we want the specific
                // member prototype to go searching for.  There could be many members called "x" which have different
                // documentation.
                final IASTName[] result = { null };
                EnclosingASTNameJob job = new EnclosingASTNameJob(t, region.getOffset(), region.getLength());
                job.schedule();
                try {
                    job.join();
                } catch (InterruptedException e) {
                    // just return
                    return null;
                }
                if (job.getResult() == Status.OK_STATUS) {
                    result[0] = job.getASTName();
                }
                if (result[0] != null) {
                    final IBinding binding = result[0].getBinding();
                    // Check to see we have a member function.
                    if (binding instanceof ICPPFunction) {
                        methodType = ((ICPPFunction) binding).getType();
                        // We have a member function, find the class name.
                        IBinding owner = ((ICPPFunction) binding).getOwner();
                        if (owner instanceof ICPPClassType) {
                            className = getClassName((ICPPClassType) owner);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // Loop through all the documents we have and report first match.
    for (int i = 0; i < helpBooks.length; ++i) {
        LibHoverLibrary l = libraries.get(helpBooks[i]);
        if (name != null) {
            if (className != null) {
                if (l.isCPP()) {
                    f = getMemberSummary(l, className, name, methodType);
                }
            } else {
                f = getFunctionSummary(l, name);
            }
            if (f != null) {
                return f;
            }
        }
    }
    return null;
}
Also used : IFunctionSummary(org.eclipse.cdt.ui.IFunctionSummary) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) IRegion(org.eclipse.jface.text.IRegion) CoreException(org.eclipse.core.runtime.CoreException) DOMException(org.eclipse.cdt.core.dom.ast.DOMException) IHoverHelpInvocationContext(org.eclipse.cdt.ui.text.IHoverHelpInvocationContext) ICPPFunction(org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction) IASTName(org.eclipse.cdt.core.dom.ast.IASTName) ICPPFunctionType(org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType) ICPPClassType(org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit)

Example 2 with IBinding

use of org.eclipse.cdt.core.dom.ast.IBinding in project linuxtools by eclipse.

the class ProfileUIUtils method findFunctionsInProject.

/**
 * Get a mapping between a file name, and the data relevant to locating
 * the corresponding function name for a given project.
 *
 * @param project : C Project Type
 * @param functionName : Name of a function
 * @param numArgs : The number of arguments this function is expected to have.
 * A value of -1 will ignore the number of arguments when searching.
 * @param fileHint : The name of the file where we expect to find functionName.
 * It is null if we do not want to use this option.
 * @return Absolute paths of files and the function's corresponding node-offset and length.
 * @since 3.0
 */
public static Map<String, int[]> findFunctionsInProject(ICProject project, String functionName, int numArgs, String fileHint) {
    HashMap<String, int[]> files = new HashMap<>();
    IIndexManager manager = CCorePlugin.getIndexManager();
    IIndex index = null;
    try {
        index = manager.getIndex(project);
        index.acquireReadLock();
        IBinding[] bindings = index.findBindings(functionName.toCharArray(), IndexFilter.ALL, null);
        for (IBinding bind : bindings) {
            if (bind instanceof IFunction && (numArgs == -1 || ((IFunction) bind).getParameters().length == numArgs)) {
                IFunction ifunction = (IFunction) bind;
                IIndexName[] names = index.findNames(ifunction, IIndex.FIND_DEFINITIONS);
                for (IIndexName iname : names) {
                    IIndexFile file = iname.getFile();
                    if (file != null) {
                        String loc = file.getLocation().getURI().getPath();
                        if (fileHint != null) {
                            if (loc.equals(new File(fileHint).getCanonicalPath())) {
                                // TODO: Consider changing data structure so that we can
                                // store multiple same-named functions (different args)
                                // from the same file.
                                files.put(loc, new int[] { iname.getNodeOffset(), iname.getNodeLength() });
                            }
                        } else {
                            files.put(loc, new int[] { iname.getNodeOffset(), iname.getNodeLength() });
                        }
                    }
                }
            }
        }
    } catch (CoreException | InterruptedException | IOException e) {
        e.printStackTrace();
    } finally {
        index.releaseReadLock();
    }
    return files;
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) HashMap(java.util.HashMap) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) IIndexName(org.eclipse.cdt.core.index.IIndexName) IOException(java.io.IOException) IIndexManager(org.eclipse.cdt.core.index.IIndexManager) IFunction(org.eclipse.cdt.core.dom.ast.IFunction) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) IIndexFile(org.eclipse.cdt.core.index.IIndexFile) File(java.io.File) IIndexFile(org.eclipse.cdt.core.index.IIndexFile)

Example 3 with IBinding

use of org.eclipse.cdt.core.dom.ast.IBinding in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class NodeWidget method displayBindings.

private void displayBindings(final IASTNode node) {
    try {
        if (node instanceof IASTName) {
            final IBinding binding = ((IASTName) node).resolveBinding();
            final TreeItem parent = createTreeItem(tree, "Bindings;");
            displayBindings(parent, node, binding);
        }
    } catch (final Exception e) {
        PastaPlugin.log(e);
    }
}
Also used : IASTName(org.eclipse.cdt.core.dom.ast.IASTName) TreeItem(org.eclipse.swt.widgets.TreeItem) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

IBinding (org.eclipse.cdt.core.dom.ast.IBinding)3 CoreException (org.eclipse.core.runtime.CoreException)3 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 DOMException (org.eclipse.cdt.core.dom.ast.DOMException)1 IFunction (org.eclipse.cdt.core.dom.ast.IFunction)1 ICPPClassType (org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType)1 ICPPFunction (org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction)1 ICPPFunctionType (org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType)1 IIndex (org.eclipse.cdt.core.index.IIndex)1 IIndexFile (org.eclipse.cdt.core.index.IIndexFile)1 IIndexManager (org.eclipse.cdt.core.index.IIndexManager)1 IIndexName (org.eclipse.cdt.core.index.IIndexName)1 ITranslationUnit (org.eclipse.cdt.core.model.ITranslationUnit)1 IFunctionSummary (org.eclipse.cdt.ui.IFunctionSummary)1 IHoverHelpInvocationContext (org.eclipse.cdt.ui.text.IHoverHelpInvocationContext)1 IFile (org.eclipse.core.resources.IFile)1