Search in sources :

Example 1 with IIndex

use of org.eclipse.cdt.core.index.IIndex in project arduino-eclipse-plugin by Sloeber.

the class IndexHelper method findParameterInFunction.

/**
 * given a project look in the source code for the line of code that sets
 * the password;
 *
 * return the password string of no_pwd_found_in_code
 *
 * @param iProject
 * @return
 */
public static String findParameterInFunction(IProject project, String parentFunctionName, String childFunctionName, String defaultValue) {
    ICProject curProject = CoreModel.getDefault().getCModel().getCProject(project.getName());
    IIndex index = null;
    try {
        index = CCorePlugin.getIndexManager().getIndex(curProject);
        index.acquireReadLock();
        IIndexBinding[] bindings = index.findBindings(parentFunctionName.toCharArray(), IndexFilter.ALL_DECLARED, new NullProgressMonitor());
        ICPPFunction parentFunction = null;
        for (IIndexBinding curbinding : bindings) {
            if (curbinding instanceof ICPPFunction) {
                parentFunction = (ICPPFunction) curbinding;
            }
        }
        if (parentFunction == null) {
            // that on found binding must be a function
            return defaultValue;
        }
        IIndexName[] names = index.findNames(parentFunction, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS);
        return findParameterInFunction(names, childFunctionName, defaultValue);
    } catch (CoreException | InterruptedException e) {
        e.printStackTrace();
    } finally {
        if (index != null) {
            index.releaseReadLock();
        }
    }
    return defaultValue;
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) IIndexBinding(org.eclipse.cdt.core.index.IIndexBinding) ICPPFunction(org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICProject(org.eclipse.cdt.core.model.ICProject) CoreException(org.eclipse.core.runtime.CoreException) IIndexName(org.eclipse.cdt.core.index.IIndexName)

Example 2 with IIndex

use of org.eclipse.cdt.core.index.IIndex in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class SourceFileBaseTest method assertEqualsWithAST.

private void assertEqualsWithAST(final String testSourceFileName, EnumSet<ComparisonArg> args, int astStyle) {
    IIndex expectedIndex = null;
    IIndex currentIndex = null;
    try {
        // TODO parallelize creation of index and ast
        expectedIndex = CCorePlugin.getIndexManager().getIndex(getExpectedCProject(), IIndexManager.ADD_DEPENDENCIES & IIndexManager.ADD_DEPENDENT);
        currentIndex = CCorePlugin.getIndexManager().getIndex(getCurrentCProject(), IIndexManager.ADD_DEPENDENCIES & IIndexManager.ADD_DEPENDENT);
        expectedIndex.acquireReadLock();
        currentIndex.acquireReadLock();
        ITranslationUnit expectedTU = CoreModelUtil.findTranslationUnit(getExpectedIFile(testSourceFileName));
        ITranslationUnit currentTU = CoreModelUtil.findTranslationUnit(getCurrentIFile(testSourceFileName));
        // ASTComparison.assertEqualsAST(expectedTU.getAST(), currentTU.getAST(), args);
        ASTComparison.assertEqualsAST(expectedTU.getAST(expectedIndex, astStyle), currentTU.getAST(currentIndex, astStyle), args);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (expectedIndex != null)
            expectedIndex.releaseReadLock();
        if (currentIndex != null)
            currentIndex.releaseReadLock();
    }
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) IOException(java.io.IOException)

Example 3 with IIndex

use of org.eclipse.cdt.core.index.IIndex 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 4 with IIndex

use of org.eclipse.cdt.core.index.IIndex in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class ASTView method getAST.

private IASTTranslationUnit getAST() {
    final IEditorInput editorInput = CUIPlugin.getActivePage().getActiveEditor().getEditorInput();
    final IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
    IIndex index;
    try {
        index = CCorePlugin.getIndexManager().getIndex(workingCopy.getCProject());
    } catch (final CoreException e) {
        throw new RuntimeException(e);
    }
    try {
        index.acquireReadLock();
        return workingCopy.getAST(index, ITranslationUnit.AST_SKIP_ALL_HEADERS).copy(IASTNode.CopyStyle.withoutLocations);
    } catch (final CoreException | InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        index.releaseReadLock();
    }
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) IWorkingCopy(org.eclipse.cdt.core.model.IWorkingCopy) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput)

Example 5 with IIndex

use of org.eclipse.cdt.core.index.IIndex in project arduino-eclipse-plugin by Sloeber.

the class Libraries method getUnresolvedProjectIncludes.

private static Set<String> getUnresolvedProjectIncludes(IProject iProject) {
    Set<String> ret = new TreeSet<>();
    ICProject tt = CoreModel.getDefault().create(iProject);
    IIndex index = null;
    try {
        index = CCorePlugin.getIndexManager().getIndex(tt);
        index.acquireReadLock();
        try {
            IIndexFile[] allFiles = index.getFilesWithUnresolvedIncludes();
            for (IIndexFile curUnesolvedIncludeFile : allFiles) {
                IIndexInclude[] includes = curUnesolvedIncludeFile.getIncludes();
                for (IIndexInclude curinclude : includes) {
                    if (curinclude.isActive() && !curinclude.isResolved()) {
                        ret.add(new Path(curinclude.getName()).removeFileExtension().toString());
                    }
                }
            }
        } finally {
            index.releaseReadLock();
        }
    } catch (CoreException e1) {
        // ignore
        e1.printStackTrace();
    } catch (InterruptedException e) {
        // ignore
        e.printStackTrace();
    }
    return ret;
}
Also used : IIndex(org.eclipse.cdt.core.index.IIndex) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICProject(org.eclipse.cdt.core.model.ICProject) CoreException(org.eclipse.core.runtime.CoreException) TreeSet(java.util.TreeSet) IIndexInclude(org.eclipse.cdt.core.index.IIndexInclude) IIndexFile(org.eclipse.cdt.core.index.IIndexFile)

Aggregations

IIndex (org.eclipse.cdt.core.index.IIndex)6 CoreException (org.eclipse.core.runtime.CoreException)4 ICProject (org.eclipse.cdt.core.model.ICProject)3 IOException (java.io.IOException)2 IIndexFile (org.eclipse.cdt.core.index.IIndexFile)2 IIndexName (org.eclipse.cdt.core.index.IIndexName)2 ITranslationUnit (org.eclipse.cdt.core.model.ITranslationUnit)2 IFile (org.eclipse.core.resources.IFile)2 IPath (org.eclipse.core.runtime.IPath)2 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)1 IFunction (org.eclipse.cdt.core.dom.ast.IFunction)1 ICPPFunction (org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction)1 IIndexBinding (org.eclipse.cdt.core.index.IIndexBinding)1 IIndexInclude (org.eclipse.cdt.core.index.IIndexInclude)1