[cfe-dev] using clang - getting type for C variable declaration

Rajendra rks at cse.iitb.ac.in
Thu Nov 8 08:35:58 PST 2012


Hi,

Using clang API, I want to get type for a declaration.

e.g. declaration is: int x; I am getting variable name as x but I am 
not able to get type of x.

If I can get clang::QualType then I can see type, but I am not getting 
how to get clang::QualType from clang::Decl

   clang::DeclGroupRef::iterator it;

   for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
   {

     clang::Decl* decl = *it;
     std::cerr << "\tdecl statement: ";
     decl->dump();        // --> this gives `int x'

     const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);

     if (namedDecl)
     {
       std::cerr << "\t\tidentifier name = "
         << namedDecl->getNameAsString() << "\n";        // --> this 
gives `x'
     }

   }

Any pointers?

Rajendra



More information about the cfe-dev mailing list