Skip to main content

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

well then what if i use something like BBCDT? 
from here: http://www.ibm.com/developerworks/library/os-ecl-cdt3/index.html

IASTCompilationUnit unit = CCorePlugin.getCompilationUnit();
Iterator iter = unit.getDeclarations();
    while(iter.hasNext()) {
        IASTDeclaration decl = (IASTDeclaration)iter.next();
       if (decl instanceof IASTVariable)
            output += "Variable declaration: " + 
                ((IASTVariable)decl).getName();
    }

But I need the scope of the variable but the IASTVariable doesn't have the
getElementype method i.e. I cannot do this : 

     ((IASTVariable) decl).getElementType == ICElement.C_VARIABLE to check
its scope. 

I need IVariable because IVariable has the method getElementType();

Thanks
A.Rahman


Doug Schaefer-2 wrote:
> 
> No. The CDT is a plug-in to Eclipse. It uses a lot of the underlying
> Eclipse
> platform to manage C/C++ projects, which requires the platform to be
> properly activated.
> 
> 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 4:08 PM
>> To: cdt-dev@xxxxxxxxxxx
>> Subject: [cdt-dev] RE: RE: Using CDT API to get C-AST
>> 
>> 
>> I am using it with the eclipse as an editor but not using the cdt as
>> plugins.
>> Rather I am using CDT only as API. I want to modify the given source and
>> get
>> the new modified one.
>> 
>> Isn't it possible to use in this way?
>> Thanks and Regards,
>> A.Rahman
>> 
>> 
>> Doug Schaefer-2 wrote:
>> >
>> > How are you running this code that you're getting a "Workspaces is
>> > closed."
>> > error? This tells me you are trying to do this from outside Eclipse.
>> >
>> > 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 2:46 PM
>> >> To: cdt-dev@xxxxxxxxxxx
>> >> Subject: [cdt-dev] RE: Using CDT API to get C-AST
>> >>
>> >>
>> >> Thank for the rapid response. The error i am facing from the second
>> block
>> >> of
>> >> code is below:
>> >>
>> >> Exception in thread "main" java.lang.ExceptionInInitializerError
>> >>
>> >>      at Itest.main(Itest.java:25)
>> >>
>> >> Caused by: java.lang.IllegalStateException: Workspace is closed.
>> >>
>> >>      at
>> >>
>> org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.ja
>> >> va:329)
>> >>
>> >>      at
>> org.eclipse.cdt.internal.core.model.CModel.<init>(CModel.java:34)
>> >>
>> >>      at org.eclipse.cdt.internal.core.model.CModelManager
>> >> .<init>(CModelManager.java:84)
>> >>
>> >>      at
>> >>
>> org.eclipse.cdt.internal.core.model.CModelManager.getDefault(CModelManager
>> >> .java:165)
>> >>
>> >>      at org.eclipse.cdt.core.model.CoreModel.<clinit>(CoreModel.java
>> :60)
>> >>
>> >>      ... 1 more
>> >>
>> >> Which is indicating the line: ITranslationUnit trUnit =
>> >> (ITranslationUnit)
>> >> cFile;
>> >>
>> >> can you please correct me if i am wrong?
>> >> after solving this problem i will get the IASTTranslationUnit by
>> >> ITranslationUnit.getAST ()
>> >> >From which i can get IASTDeclaration by
>> >> IASTTranslationUnit.getDeclarations();
>> >> i can get IName by IASTTranslationUnit.getDeclarations(Binding);
>> >> (guessing
>> >> i
>> >> somehow know the global variable binding)
>> >> or i can get global IScope by IASTTranslationUnit.getScope();
>> >>
>> >> But which of this will help me to get the global variable declarations
>> >> and
>> >> the statements which are using it?
>> >>
>> >> Any Help on how can i edit those statement nodes and unparse/Pretty
>> Print
>> >> them?
>> >>
>> >> Thanks again.
>> >> Regards,
>> >> 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#a10899752
>> >> 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#a10901109
>> 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#a10906451
Sent from the Eclipse CDT - Development mailing list archive at Nabble.com.



Back to the top