当前位置: 首页 > 知识库问答 >
问题:

我无法使用cdt插件eclipse从c源代码获取AST

弘烨烁
2023-03-14

我需要使用getAST()方法从源文件中获取AST。我创建了一个.cproject,因为我想如果它是丢失的那个,但它还是犯了同样的错误。在代码的这一部分中:

ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault (). Create (iFile);
    public void analyzeFilesInSrc() throws Exception{
        ICProject project = CoreModel.getDefault().getCModel().getCProject(SampleHandler.PROJECT);//project to analyse
        IIndex index = CCorePlugin.getIndexManager().getIndex(project);
        System.out.println("iindex "+index);

        // It gets all C files from the SRC path to analyze.
        this.filesInSrc = new ArrayList<String>();
        this.setSrcFiles(SampleHandler.RUNTIME_WORKSPACE_PATH + SampleHandler.PROJECT + File.separator + "src");

        // For each C file in the SRC folder..
        for (String file : this.filesInSrc){
            String completeFilePath = file.replace(SampleHandler.RUNTIME_WORKSPACE_PATH, "");

            System.out.println(completeFilePath.replace(PROJECT + File.separator + "src" + File.separator, ""));
            this.editDirectives(file);

            IPath iFilePath = new Path(completeFilePath);
            IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(iFilePath);

            System.out.println("ifile "+iFile);
            System.out.println("Itranslatiotounit "+(ITranslationUnit) CoreModel.getDefault().create(iFile));
            ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(iFile);

            System.out.println("tu "+tu);
            try {
                // We need a read-lock on the index.
                index.acquireReadLock();
                IASTTranslationUnit ast= tu.getAST(index, ITranslationUnit.AST_PARSE_INACTIVE_CODE);

                this.setTypes(ast);
                this.setMacros(ast);
}
java.lang.NullPointerException
    at analysis.handlers.SampleHandler.analyzeFilesInSrc(SampleHandler.java:249)
    at analysis.handlers.SampleHandler.execute(SampleHandler.java:103)
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:295)
    at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

共有1个答案

闾丘晨
2023-03-14

手动创建.cproject文件是不可行的-它的格式没有文档化,被认为是一个实现细节。您应该让Eclipse创建该文件,以及任何其他项目元数据文件,作为创建实际项目的一部分。

实际项目的创建可以在Eclipse UI中手动完成(file New C++project),也可以使用诸如iWorkspaceroot.getProject()IProject.create()coRemodel.create(IProject)等API来自动创建。

 类似资料:
  • 我正在开发一个Eclipse插件,其中必须检查当前C项目中设置了哪些编译器选项。基本上,我想访问属性->C/C++Build->Settings->GCC C Compiler->All options字段。 我已经搜索了如何访问它,但我还没有找到一种方法。我试图通过首选项访问它,如下代码所示: 然后我就可以解析字符串了,虽然不是很理想,但它可以工作。我从这篇文章中获得了getResourceIn

  • 它会引发一个异常:未能启动eclipse以导入项目。确保'eclipse'在您的路径中,然后再试一次 我试图将Eclipse的目录(applications/eclipse.app)添加到我的PATH环境变量中,但仍然得到相同的错误。 以下是完整的跟踪: 0:00.46/users/****/src/mozilla-central/obj-ff-dbg/_virtualenvs/init/bin/

  • 问题内容: 如何从或返回的Eclipse中当前打开的文件中获取代码?我正在制作的插件需要这个。 假设我有以下代码: 如果我打开HelloWorld.java,如何将代码返回到中?在将包含: “公共类HelloWorld { “公共静态无效主(String [] args){” “ System.out.println(” Hello,world!“);” “}” “}” 问题答案: 要获取当前编辑

  • 如何从Eclipse中当前打开的文件中获取以或返回的代码?我需要这个插件我正在做。 假设我有以下代码: 如果我打开了HelloWorld.java,如何获得中返回的代码?将包含: “公共类HelloWorld{” “Public static void main(String[]args){” “System.out.println(”Hello,World!“);” “}” “}”

  • 我正在使用Eclipse Plugin-in-project开发一个Eclipse插件,它将在工具栏中添加一个菜单项。 请帮我解决这个问题!

  • 我哪里做错了?