Skip to main content

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

As you said error was caused for not using the CDT as plugins, then what is
the proper way of using this piece of code without any error?

This is the code which caused " java.lang.IllegalStateException: Workspace
is closed."
Here file.cc is the path to the source file (an object of IPath).

ICElement cFile = CoreModel.getDefault().create(file.cc);
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());
           }    
      }        
} 

Thanks
A.Rahman


Doug Schaefer-2 wrote:
> 
> The proper way to get an AST now (I think, I'm starting to lose track as
> I've been busy up in build/debug-land), is off the ITranslationUnit using
> getAST().
> 
> As for your exception, what was the exception? That's important
> information.
> Was it a ClassCastException? That would mean your IFile wasn't a
> ITranslationUnit. You should always check the types before doing a cast.
> 
> Doug Schaefer, QNX Software Systems
> Eclipse CDT Project Lead, http://cdtdoug.blogspot.com
> 
> 
>> -----Original Message-----
>> From: cdt-dev-bounces@xxxxxxxxxxx [mailto:cdt-dev-bounces@xxxxxxxxxxx] On
>> Behalf Of rainman
>> Sent: Thursday, May 31, 2007 1:58 PM
>> To: cdt-dev@xxxxxxxxxxx
>> Subject: [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.
>> 
>> _______________________________________________
>> cdt-dev mailing list
>> cdt-dev@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/cdt-dev
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/cdt-dev
> 
> 

-- 
View this message in context: http://www.nabble.com/Using-CDT-API-to-get-C-AST-tf3847998.html#a10907197
Sent from the Eclipse CDT - Development mailing list archive at Nabble.com.



Back to the top