Search in sources :

Example 1 with IIndexManager

use of org.eclipse.cdt.core.index.IIndexManager in project linuxtools by eclipse.

the class AbstractTest method createProject.

protected ICProject createProject(Bundle bundle, String projname) throws CoreException, URISyntaxException, IOException, InvocationTargetException, InterruptedException {
    // Turn off auto-building
    IWorkspace wsp = ResourcesPlugin.getWorkspace();
    IWorkspaceDescription desc = wsp.getDescription();
    desc.setAutoBuilding(false);
    wsp.setDescription(desc);
    ICProject proj = CProjectHelper.createCProject(projname, BIN_DIR);
    URL location = FileLocator.find(bundle, new Path("resources/" + projname), // $NON-NLS-1$
    null);
    File testDir = new File(FileLocator.toFileURL(location).toURI());
    IProject project = proj.getProject();
    // Add these natures before project is imported due to #273079
    ManagedCProjectNature.addManagedNature(project, null);
    ScannerConfigNature.addScannerConfigNature(project);
    ImportOperation op = new ImportOperation(project.getFullPath(), testDir, FileSystemStructureProvider.INSTANCE, pathString -> IOverwriteQuery.ALL);
    op.setCreateContainerStructure(false);
    op.run(null);
    IStatus status = op.getStatus();
    if (!status.isOK()) {
        throw new CoreException(status);
    }
    // Index the project
    IIndexManager indexManager = CCorePlugin.getIndexManager();
    indexManager.reindex(proj);
    indexManager.joinIndexer(IIndexManager.FOREVER, new NullProgressMonitor());
    // These natures must be enabled at this point to continue
    assertTrue(project.isNatureEnabled(ScannerConfigNature.NATURE_ID));
    assertTrue(project.isNatureEnabled(ManagedCProjectNature.MNG_NATURE_ID));
    return proj;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) IWorkspaceDescription(org.eclipse.core.resources.IWorkspaceDescription) ICProject(org.eclipse.cdt.core.model.ICProject) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) ImportOperation(org.eclipse.ui.wizards.datatransfer.ImportOperation) IIndexManager(org.eclipse.cdt.core.index.IIndexManager) LocalFile(org.eclipse.core.internal.filesystem.local.LocalFile) File(java.io.File) URL(java.net.URL) IProject(org.eclipse.core.resources.IProject)

Example 2 with IIndexManager

use of org.eclipse.cdt.core.index.IIndexManager in project linuxtools by eclipse.

the class AbstractTest method createExternalProject.

/**
 * Create a CDT project outside the default workspace.
 *
 * @param bundle
 *            The plug-in bundle.
 * @param projname
 *            The name of the project.
 * @param absProjectPath
 *            Absolute path to the directory to which the project should be
 *            mapped outside the workspace.
 * @return A new external CDT project.
 * @throws CoreException
 * @throws URISyntaxException
 * @throws IOException
 * @throws InvocationTargetException
 * @throws InterruptedException
 */
protected IProject createExternalProject(Bundle bundle, final String projname, final Path absProjectPath) throws CoreException, URISyntaxException, IOException, InvocationTargetException, InterruptedException {
    IProject externalProject;
    // Turn off auto-building
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceDescription wspDesc = workspace.getDescription();
    wspDesc.setAutoBuilding(false);
    workspace.setDescription(wspDesc);
    // Create external project
    IWorkspaceRoot root = workspace.getRoot();
    externalProject = root.getProject(projname);
    IProjectDescription description = workspace.newProjectDescription(projname);
    URI fileProjectURL = new URI("file://" + absProjectPath.toString());
    description.setLocationURI(fileProjectURL);
    externalProject = CCorePlugin.getDefault().createCDTProject(description, externalProject, new NullProgressMonitor());
    assertNotNull(externalProject);
    externalProject.open(null);
    try {
        // CDT opens the Project with BACKGROUND_REFRESH enabled which
        // causes the
        // refresh manager to refresh the project 200ms later. This Job
        // interferes
        // with the resource change handler firing see: bug 271264
        Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null);
    } catch (Exception e) {
    // Ignore
    }
    assertTrue(externalProject.isOpen());
    // Import boiler-plate files which can then be built and profiled
    URL location = FileLocator.find(bundle, new Path("resources/" + projname), // $NON-NLS-1$
    null);
    File testDir = new File(FileLocator.toFileURL(location).toURI());
    ImportOperation op = new ImportOperation(externalProject.getFullPath(), testDir, FileSystemStructureProvider.INSTANCE, new IOverwriteQuery() {

        @Override
        public String queryOverwrite(String pathString) {
            return ALL;
        }
    });
    op.setCreateContainerStructure(false);
    op.run(null);
    IStatus status = op.getStatus();
    if (!status.isOK()) {
        throw new CoreException(status);
    }
    // Make sure import went well
    assertNotNull(externalProject.findMember(new Path(IMPORTED_SOURCE_FILE)));
    // Index the project
    IIndexManager indexMgr = CCorePlugin.getIndexManager();
    indexMgr.joinIndexer(IIndexManager.FOREVER, new NullProgressMonitor());
    // These natures must be enabled at this point to continue
    assertTrue(externalProject.isNatureEnabled(ScannerConfigNature.NATURE_ID));
    assertTrue(externalProject.isNatureEnabled(ManagedCProjectNature.MNG_NATURE_ID));
    return externalProject;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IOverwriteQuery(org.eclipse.ui.dialogs.IOverwriteQuery) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) ImportOperation(org.eclipse.ui.wizards.datatransfer.ImportOperation) IIndexManager(org.eclipse.cdt.core.index.IIndexManager) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) URL(java.net.URL) IWorkspaceDescription(org.eclipse.core.resources.IWorkspaceDescription) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) LocalFile(org.eclipse.core.internal.filesystem.local.LocalFile) File(java.io.File)

Example 3 with IIndexManager

use of org.eclipse.cdt.core.index.IIndexManager 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)3 IIndexManager (org.eclipse.cdt.core.index.IIndexManager)3 CoreException (org.eclipse.core.runtime.CoreException)3 IOException (java.io.IOException)2 URL (java.net.URL)2 LocalFile (org.eclipse.core.internal.filesystem.local.LocalFile)2 IProject (org.eclipse.core.resources.IProject)2 IWorkspace (org.eclipse.core.resources.IWorkspace)2 IWorkspaceDescription (org.eclipse.core.resources.IWorkspaceDescription)2 IPath (org.eclipse.core.runtime.IPath)2 IStatus (org.eclipse.core.runtime.IStatus)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 ImportOperation (org.eclipse.ui.wizards.datatransfer.ImportOperation)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)1 IFunction (org.eclipse.cdt.core.dom.ast.IFunction)1