Skip to main content

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

Hi Sonia

To solve your problem, I think that you have to use the Indexer and
AST-Structure provided by CDT. I try to give you some hints in the
following (incomplete) code:


IIndex index = CCorePlugin.getIndexManager().getIndex(project);
// you should lock and unlock the index before and after use
int astStyle = ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT |
ITranslationUnit.AST_SKIP_INDEXED_HEADERS;
IASTTranslationUnit astTu = translationUnit.getAST(index, astStyle);
//use a CPPASTVisitor (you have to subclass it) to find classes
//(which in the ast are ICPPASTCompositeTypeSpecifier)
//to find class source code use
compTypeSpecifier.getRawSignature();
//to find its methods use
ICPPClassType classBinding = (ICPPClassType)
compTypeSpecifier.resolveBinding();
classBinding.getMethods();
index.findNames(x, IIndex.FIND_DEFINITIONS);
//this gives you the IName of the method definition.
//you should be able to use the name's IASTFileLocation to get
//information about the whereabout of the method's definition

I hope this information helps you to solve your problem.

Lukas


On Tue, 2010-11-09 at 20:50 +0100, HAIDUC SONIA wrote:
> 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