在我的源代碼分析Eclipse RCP的項目,我想getAST分析一些C/C++文件的AST C/C++文件的AST ,它既不是eclipse工作區內項目的源文件,也不是eclipse工作區內項目的鏈接資源。基本上,我的RCP應用程序中沒有任何工作空間。任何建議,將不勝感激!CDT:如何分析這些沒有聯繫的一些項目在Eclipse工作區
歡呼聲,
在我的源代碼分析Eclipse RCP的項目,我想getAST分析一些C/C++文件的AST C/C++文件的AST ,它既不是eclipse工作區內項目的源文件,也不是eclipse工作區內項目的鏈接資源。基本上,我的RCP應用程序中沒有任何工作空間。任何建議,將不勝感激!CDT:如何分析這些沒有聯繫的一些項目在Eclipse工作區
歡呼聲,
您需要以編程方式從來源(請確保您有內部進行正確的源根的一些基本.cproject文件)文件夾中創建新的項目:
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);
}
後,您可以使用AST :
ITranslationUnit translationUnit = (ITranslationUnit) CoreModel.getDefault().create(file);
IASTTranslationUnit ast = translationUnit.getAST();
我發現了一個更簡單的方法,如果你碰巧在編輯器中該文件,然後你可以使用編輯器部分getEditorInput方法來獲取ITranslationUnit,即:
// 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(); }
歡迎StackOverflow的aayupov。你必須看看[幫助中心](http://stackoverflow.com/help)。其實這裏你正在回答2年前問的問題。雖然它沒有錯。 :) – afzalex