在java环境中解析C ++源代码[关闭]

问题描述 投票:6回答:6

我有一个C ++应用程序,我也有它的源代码,它建立在.Net框架上。我现在正在寻找的是一个工具(理想上是免费的),它将我的源代码作为输入,经过一些预处理或一些必需的编程,给我一个源代码元素的结构依赖的中间表示,如AST或一个调用图。我正在使用java来检查这个C ++源代码,所以如果解决方案在java环境中就像eclipse或其他东西的插件那样会好得多。是否有适合我需要的工具?谢谢你们。

java c++ static-analysis abstract-syntax-tree
6个回答
4
投票

我已经成功使用NetBeans中的CND模块在Java中解析C ++。它仍然是丑陋的,但可能比使用原始的ANTLR或更好。首先,为了简单起见,从http://netbeans.org/downloads/zip.html下载“all-in-one”软件包(CND实际上不需要所有这些类)并解压缩在当前目录中的某个位置。接下来,这是我一直在玩的玩具C ++头文件:

namespace foo {

int f(int p);

template<typename A> class bar {
    void run(A) { }
};

}

这是我尝试使用CND解析它:

import java.io.*;
import java.util.*;
import org.openide.filesystems.*;
import org.netbeans.modules.cnd.api.model.*;
import org.netbeans.modules.cnd.api.model.services.*;
import org.netbeans.modules.cnd.modelimpl.csm.*;

public class Foo {
    public static void main(String[] args) throws Exception {
        FileObject fo = FileUtil.toFileObject(new File(args[0]));
        CsmStandaloneFileProvider fp = CsmStandaloneFileProvider.getDefault();
        CsmModel model = CsmModelAccessor.getModel();
        CsmModelState modelState = CsmModelAccessor.getModelState();

        CsmFile cf = fp.getCsmFile(fo);
        cf.scheduleParsing(true);
        Collection<CsmOffsetableDeclaration> c = cf.getDeclarations();
        c = ((CsmNamespaceDefinition)c.toArray()[0]).getDeclarations();
        for (CsmOffsetableDeclaration d : c) {
            if (d instanceof CsmFunction) {
                CsmFunction f = (CsmFunction)d;
                System.out.print(f.getQualifiedName() + " " + f.getName() + "(");
                Collection<CsmParameter> pp = f.getParameters();
                for (CsmParameter p : pp) {
                    System.out.print(p.getType().getClassifierText());
                }
                System.out.println(")");
            } else if (d instanceof ClassImpl) {
                ClassImpl cls = (ClassImpl)d;
                System.out.println("Got template? " + cls.isTemplate());
                List<CsmTemplateParameter> lt = cls.getTemplateParameters();
                for (CsmTemplateParameter t : lt) {
                    System.out.println(t.getQualifiedName() + " " + t.getName());
                }
                Collection<CsmMember> cm = cls.getMembers();
                for (CsmMember m : cm) {
                    CsmFunction f = (CsmFunction)m;
                    System.out.print(f.getQualifiedName() + " " + f.getName() + "(");
                    Collection<CsmParameter> pp = f.getParameters();
                    for (CsmParameter p : pp) {
                        System.out.print(p.getType().getClassifierText());
                    }
                    System.out.println(")");
                }
            }
        }
    }
}

我们需要添加到类路径中的JAR文件数量非常大,并且必须有更好的方法来处理这个问题,但现在这样做:

$ export CLASSPATH=netbeans/platform/modules/org-netbeans-modules-editor-mimelookup.jar:netbeans/platform/modules/org-netbeans-modules-queries.jar:netbeans/dlight/modules/org-netbeans-modules-dlight-libs-common.jar:netbeans/ide/modules/org-netbeans-modules-projectapi.jar:netbeans/platform/modules/org-netbeans-api-progress.jar:netbeans/platform/modules/org-openide-windows.jar:netbeans/platform/modules/org-openide-text.jar:netbeans/platform/modules/org-openide-awt.jar:netbeans/platform/lib/org-openide-modules.jar:netbeans/platform/modules/org-openide-nodes.jar:netbeans/platform/modules/org-netbeans-modules-masterfs.jar:netbeans/platform/core/org-openide-filesystems.jar:netbeans/platform/lib/org-openide-util.jar:netbeans/platform/lib/org-openide-util-lookup.jar:netbeans/platform/modules/org-openide-loaders.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-api-model.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-api-project.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-model-services.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-modelimpl.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-modelutil.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-utils.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-repository.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-repository-api.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-apt.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-source.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-antlr.jar:.
$ javac Foo.java
$ java Foo Foo.h

并输出以下内容:

foo::f f(int)
Got template? true
foo::bar::A A
foo::bar::run run(A)

顺便说一下,我们可以用Eclipse CDT做类似的事情:Parsing / reading C-Header files using Java


4
投票

您可能对doxygen感兴趣,它是一个基于源代码生成文档的免费工具。无需任何额外的工作,您可以生成调用图,继承图,协作图和许多其他有用的工具。

http://www.doxygen.nl/


1
投票

您可以使用在纯Java中实现的Eclipse Parser,并且只有2个jar。我已经提供了如何使用的详细信息,请参阅链接: https://stackoverflow.com/a/27496664/955857

上面的链接还有一个项目,它使Eclipse Parser的抽象带来了一个更简单的结构(但不适合所有情况)


0
投票

根据您的要求,概要可能对您有意义:http://synopsis.fresco.org/

这是一个模块化工具,可解析源代码(C / C ++ / Python / IDL)并生成抽象语法图。该图可以通过API遍历(或者可以传递给其他概要模块以生成源文档)。概要提供了C ++和Python API,遗憾的是没有Java API - 但我想它可以从Java通过Jython(或当然是JNI ;-))使用


0
投票

我想你应该看看Xogastan。它为C ++生成AST作为XML文档。 Xogastan有很多代的选择。

Xogastan home

问候,

编辑:它与Java没有直接关联,但您可以将其用作外部工具,然后分析生成的XML。


0
投票

Samuel Audet是对的!但他缺少JAR文件。您需要添加以下JAR文件:org-netbeans-modules-cnd-indexing.jar,org-netbeans-modules-parsing-lucene.jar,org-netbeans-libs-lucene.jar和lucene-core-3.5.0 。罐。

© www.soinside.com 2019 - 2024. All rights reserved.