Search in sources :

Example 1 with IFunction

use of org.eclipse.cdt.core.dom.ast.IFunction 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)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)1 IFunction (org.eclipse.cdt.core.dom.ast.IFunction)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 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1