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;
}
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;
}
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;
}
Aggregations