I have a C++ application and I also have the source code of that and it has been built on .Net framework. what I'm looking for now is a tool (ideally free) that gets my source code as an input, and after some preprocessing or some required programming gives me one intermediate representation of the structural dependency of the source code elements, like an AST or a call graph. I am working with java to inspect this C++ source code so it would be much better if the solution is in java environment like a plugin for eclipse or something. is there any available tool suited my need? thank you all.
-
1worst case, you could use C++ tools: http://stackoverflow.com/a/2318476/839436 – Tom Kerr Oct 12 '12 at 19:15
-
eclipse and netbeans have open source plugins for C++. You could start with those. – Peter Lawrey Oct 12 '12 at 19:16
-
It's a "simple matter" of implementing a C++ parser. (Unfortunately, C++ is one of the ugliest architectures there is to parse.) – Hot Licks Oct 12 '12 at 19:19
6 Answers
You might be interested in doxygen, it's a free tool that generates documentation based on source code. Without any additional work on your part you can generate call graphs, inheritance diagrams, collaboration diagrams, and many other useful tools.

- 8,285
- 3
- 19
- 32

- 418
- 3
- 13
I've had success parsing C++ in Java using the CND module from NetBeans. It's still butt ugly, but probably better than using raw ANTLR or what not. First, download the "all-in-one" package from http://netbeans.org/downloads/zip.html for simplicity (CND doesn't actually require all those classes) and unzip that somewhere like in the current directory. Next, here's a toy C++ header file I've been playing with:
namespace foo {
int f(int p);
template<typename A> class bar {
void run(A) { }
};
}
And here's my attempt at parsing that with 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(")");
}
}
}
}
}
The amount of JAR files we need to add to the classpath is quite large, and there must be a better way to deal with this, but this works for now:
$ 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
And that outputs the following:
foo::f f(int)
Got template? true
foo::bar::A A
foo::bar::run run(A)
BTW, we can do something similar with Eclipse CDT: Parsing / reading C-Header files using Java

- 1
- 1

- 4,964
- 1
- 26
- 33
-
BTW, today I would recommend using Clang from Java: https://github.com/bytedeco/javacpp-presets/tree/master/llvm/samples/src/main/java/org/bytedeco/javacpp/samples/clang – Samuel Audet Dec 09 '18 at 13:50
You can use the Eclipse Parser that is implemented in pure Java and are only 2 jars.
I have given details of how to use, see the link:
https://stackoverflow.com/a/27496664/955857
The link above also has a project that makes a abstraction of Eclipse Parser bringing a simpler structure (but does not fit all cases)

- 1
- 1

- 549
- 5
- 10
Depending on your requirements, Synopsis could be interesting for you: http://synopsis.fresco.org/
This is a modular tool which parses source code (C/C++/Python/IDL) and generates an abstract syntax graph. The graph can be traversed through an API (or it can be passed to other synopsis modules to generate source documentation, for instance). Synopsis provides C++ and Python APIs, unfortunately no Java API - but I suppose it could be used from Java through Jython (or JNI of course ;-) )

- 36,091
- 7
- 95
- 123
I think that you should check out Xogastan. It generates AST for C++ as a XML document. Xogastan has a lot of options of generation.
Greetings,
EDIT: It is not directly connected with Java, but you can use it as an external tool and then analyse generated XML.

- 966
- 1
- 21
- 47
Samuel Audet is right! but he is missing JAR files. You need to add the following JAR files: org-netbeans-modules-cnd-indexing.jar, org-netbeans-modules-parsing-lucene.jar, org-netbeans-libs-lucene.jar and lucene-core-3.5.0.jar.

- 1