using clang - getting type for C variable declaration


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?


You can use ValueDecl::getType.