Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Eclipse CDT - Get the definition of a C++ method when I have the declaration

Hi there,

I am trying to extract the source code of all C++ classes from  a C++ program, and I need to get, for each class, its whole source code, i.e., with both the definition of the class and the declaration of its methods from a header file and the definition of its methods from a source file. I am able to get the definition of the class, including the declaration of its methods, by using the following code:

HashSet<ITranslationUnit> transUnits = new HashSet<ITranslationUnit>();
ISourceRoot roots[] = project.getSourceRoots();
for (ISourceRoot root:roots){
transUnits.addAll(Arrays.asList(root.getTranslationUnits()));
}
for (ITranslationUnit translationUnit:transUnits){
if (translationUnit.isCXXLanguage()){
ICElement elems[] = TypeUtil.getTypes(translationUnit);
for (int i=0;i<elems.length;i++)
if (TypeUtil.isClass(elems[i])){
String classDeclarationCode = ((IStructure)elem).getSource();
}
}
}

This gets me the definitions of classes from header files, including the declarations of their methods. However, I still need to get the definition of the class' methods, which are not in the header file. I have been going around this for several days now, and I could not find a solution to this. 

I managed to get the method declarations from the header file, by using the following code:

List<ICElement> methodDeclarationsClass = new ArrayList<ICElement>();
methodDeclarationsClass.addAll(((IStructure)elem).getChildrenOfType(ICElement.C_METHOD_DECLARATION));
String methodDeclarationCode;
for (int i = 0; i<methodDeclarationsClass.size(); i++) {
methodDeclarationCode = ((IMethodDeclaration)methodDeclarationsClass.get(i)).getSource();
}

, but I can't get form this to the method definitions... I would really appreciate some help on this, as I am kinda stuck... 

Thanks a lot!

Sonia



Back to the top