1

In my source code analysis Eclipse RCP project, I want to getAST to analyze the AST of some c/c++ files, which is neither a source file of a project within an eclipse workspace, nor a link resources of a project within an eclipse workspace. Basically, I do not have any workingspaces in my RCP application. Any suggestions would be appreciated!

cheers,

Yi.
  • 515
  • 1
  • 7
  • 19

2 Answers2

0

You need to programmatically create new project from folder of sources (make sure you have some basic .cproject file inside with correct source root):

IWorkspace workspace = ResourcesPlugin.getWorkspace();
project = workspace.getRoot().getProject("project");
if (!project.exists()) {
    IProjectDescription description = workspace.newProjectDescription("project");
    CCorePlugin.getDefault().createCDTProject(description, project, null);
} else {
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
}

After you can use AST:

ITranslationUnit translationUnit = (ITranslationUnit) CoreModel.getDefault().create(file);
IASTTranslationUnit ast = translationUnit.getAST();
Manowar
  • 1
  • 1
0

I found a simpler way if you happen to have that file in the editor, then you can use getEditorInput method on the editor part to get ITranslationUnit, i.e.:

// here is how you can get the active editor IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); IEditorPart editorPart = window.getActivePage().getActiveEditor();

// for an external file the editor input will be of type ITranslationUnitEditorInput IEditorInput input = editorPart.getEditorInput(); if (input instanceof ITranslationUnitEditorInput) { ITranslationUnit externalTU = ((ITranslationUnitEditorInput) input).getTranslationUnit(); }

aayupov
  • 267
  • 1
  • 10
  • Welcome to StackOverflow aayupov. You must have a look at [help center](http://stackoverflow.com/help). Actually here you are answering to a question which is asked 2 years ago. Although there is nothing wrong in it. :) – afzalex Aug 29 '14 at 00:02