Can't get ITranslationUnit for creating an AST of an C-File within a Eclipse Plugin

本秂侑毒 提交于 2019-12-12 03:53:44

问题


I'm trying to generate an AST with help of the CDT Eclipse Framework from an C-File of a generated project in my workspace. But every time i try to get an TranslationUnit through

ICElement ice = CoreModel.getDefault().create(file);
ITranslationUnit tu= (ITranslationUnit) ice;

ice and tu are null and i get the following error

java.lang.NullPointerException
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.expandLocation(TeamPDOMImportOperation.java:135)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.getImportLocation(TeamPDOMImportOperation.java:126)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.run(TeamPDOMImportOperation.java:92)
at org.eclipse.cdt.internal.core.pdom.PDOMManager.createIndexer(PDOMManager.java:600)
at org.eclipse.cdt.internal.core.pdom.PDOMSetupJob.run(PDOMSetupJob.java:58)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

I have an Eclipse Plugin which implements an IApplication extension. In the run Method i have following code.

    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject("NewProject");
    try {
        if(project.exists()){
            if(project.isOpen())
                project.close(null);
            project.delete(false, false, progressMonitor);
            project.create(progressMonitor);
            project.open(progressMonitor);
        }
        else{
            project.create(progressMonitor);
            project.open(progressMonitor);
        }           

        if(!project.isNatureEnabled(org.eclipse.cdt.core.CProjectNature.C_NATURE_ID)){
            CProjectNature.addCNature(project, null);
        }

        project.close(null);
        project.open(null);

    } catch (CoreException e1) {
        e1.printStackTrace();
    }

    IFile file = project.getFile("stub.c");           
    try {
        IPath workspacerawlocation = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
        FileInputStream fileStream = new FileInputStream(workspacerawlocation.append("stub.h").toString());
        if(!file.exists())
            file.create(fileStream, false, null);

        ICElement ice = CoreModel.getDefault().create(file);
        if(ice == null)
            System.out.println("ice is null");
        ITranslationUnit tu= (ITranslationUnit) ice;
        if(tu == null){
            System.out.println("tu is null");
        }
        else{
            IASTTranslationUnit ast =  tu.getAST();
        }

    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

Can anyone help? Did i miss some Plugin-dependencies or initializations?


回答1:


Following up to my comment: in order to do some code manipulation, you have to have the CDT indexer working as it is required by C Model/AST. I suggest two possible ways of creating a C project configuration:

1] Look here for list of tests in org.eclipse.cdt.core.tests which create CDT projects. These projects contain very simple .cproject configuration, but some alterations to your needs might be required.

2] Save a .cproject from a working C project somewhere and when creating your projects, just copy it into. You may need to refresh the project somehow or close/reopen it so that CDT reloads the configuration.



来源:https://stackoverflow.com/questions/15548938/cant-get-itranslationunit-for-creating-an-ast-of-an-c-file-within-a-eclipse-plu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!