[cfe-dev] Decl iterators

Douglas Gregor dgregor at apple.com
Mon Feb 14 20:31:39 PST 2011


On Feb 14, 2011, at 5:06 AM, silhape2 at fel.cvut.cz wrote:

> Dear clang developers,
> 
> I have a problem when I am trying to do some analysis with clang. I  
> need to get all typedef decls that are inside the class or struct. I  
> need this for my bachelor thesis. The part of source code of the file  
> I want to analyse is:
> 
> class state_1 : sc :: simple_state< state_1, DU>
> {
>     state_1() { cout<<"Chcete zformatovat vas disk (a/n) ? \n";}
>     ~state_1() {}
>     typedef sc::transition< EvN, state_2 > reactions;
> };
> 
> It is no problem to get constructor and destructor and other methods  
> in the class. The problem is to reach the typedef decl.
> 
> The only way I found was to create my own iterator and to put it into  
> the header files of clang(DeclCXX.h). I created the iterator by using  
> this code:
> 
> typedef specific_decl_iterator<TypedefDecl> typedef_iterator;
> 
>   /// it_begin - Typedef begin iterator.  Iterates in the order the Typedefs
>   /// were declared.
>   typedef_iterator td_begin() const {
>     return typedef_iterator(decls_begin());
>   }
>   /// it_end - Typedef end iterator.
>   typedef_iterator td_end() const {
>     return typedef_iterator(decls_end());
>   }
> 
> My question is whether there is a better way how to iterate through  
> all typedef decls inside the class without updating header files of  
> clang?

For each iterator D in (decls_begin(), decls_end()), use:

  if (TypedefDecl *Typedef = llvm::dyn_cast<TypedefDecl>(*D)) {
    // Handle the typedef
  }

> If it isn't could I ask your to add this part of code to your  
> header files?(To header file DeclCXX.h).

Using dyn_cast should work well for you; there's no reason to add this specific iterator into Clang.

	- Doug



More information about the cfe-dev mailing list