Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Using CDT API to get C-AST

Hello All,
 I am new to CDT and trying to do something with cdt since last 1 month. I
have searched many sites and mailed in cdt news portal as well. Please help
me some one.

>From this list I found some thing like below but failed to get the
declaration of class to pass in IFile:
 IFile file = ...
 IASTTranslationUnit ast =
 CDOM.getInstance().getTranslationUnit(file);
 CPPNameCollector col = new CPPNameCollector();
 ast.accept(col);
            
 Iterator i = col.nameList.iterator();
 while (i.hasNext()) {
    IASTName n = (IASTName) i.next();
    IBinding binding = n.resolveBinding();
    
    if(binding instanceof CPPClassType) {
       // do something with the class
    }
 }


Also from another email I got something like below but when i pass Path(file
location) it gives some exception with workspace and exits at first line of
if block.

ICElement cFile = CoreModel.getDefault().create(IPath);
if(cFile != null)
{    
    ITranslationUnit trUnit = (ITranslationUnit) cFile;
    ICElement[] elts = trUnit.getChildren();
    if(elts != null)
     {    
         for(int i=0; i<elts.length; i++)
          {    
                System.out.println(elts[i].getElementName());
                 System.out.println(elts[i].getElementType());
           }    
      }        
} 

Any simple idea or link indication will be appreciable. I've been trying
with the CDOM cause according to Doug Schaefer that is the starting point
for creating AST. I want to traverse the AST to get the idea which
statements using global variable. Change those statements and unparse to get
the changed source.
For this i created and searched the cdt doc. I got the idea that i need to
check:

IDeclaration.getElementType() == ICElement.C_VARIABLE

To get the global variable type. But couldn't the idea how to get the
IDeclaration from the IASTDeclaration (if i can successfully get ast that is
:-).

Also i got some knowledge that in IDeclaration there is ISourceManipulation
which might be in help for ast modification. But not sure about the AST
unparse or pretty printer help yet.

Thank you for your patience.
Hope to get some positive and fast response.
Regards.
A.Rahman
-- 
View this message in context: http://www.nabble.com/Using-CDT-API-to-get-C-AST-tf3847998.html#a10898933
Sent from the Eclipse CDT - Development mailing list archive at Nabble.com.



Back to the top