Hi,
1) I need to get type as string from Expr object. Any pointers?
Expr* lhs = E->getLHS();
if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
{
// get name and type for Expr *lhs from DeclRefExpr
const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);
const ValueDecl *valueDecl = declRefExpr->getDecl();
if (valueDecl)
{
std::cerr << "\tLHS identifier = " << valueDecl->getNameAsString() << "\n"; // this gives x
QualType declQT = valueDecl->getType();
std::cerr << "\tqual type: ";
declQT.dump(); // this prints : int identifier
// now call QualType.getAsString(PrintingPolicy &Policy)
// ---> how to get compiler instance and AST context? <---- //
clang::ASTContext &context = compilerInstance_ptr->getASTContext();
std::cerr << "\ttype: " << declQT.getAsString(context.getPrintingPolicy()) << "\n";
}
}
2) Here is how I got things right for Decl
this will be helpful for others as well.
clang::DeclGroupRef::iterator it;
for (it = declGroupRef.begin(); it != declGroupRef.end(); it++)
{
clang::Decl* decl = *it;
const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
if (namedDecl)
{
std::cerr << "\tidentifier name = "
<< namedDecl->getNameAsString() << "\n";
}
const ValueDecl *valueDecl = dyn_cast<ValueDecl>(decl);
if (valueDecl)
{
QualType declQT = valueDecl->getType();
// now call QualType.getAsString(PrintingPolicy &Policy)
clang::ASTContext &context = decl->getASTContext();
std::cerr << "\ttype = " << declQT.getAsString(context.getPrintingPolicy()) << "\n";
}
}
Rajendra
Hi,
1) I need to get type as string from Expr object. Any pointers?
Presumably Expr::getType():
http://clang.llvm.org/doxygen/classclang_1_1Expr.html#a3dd8850a4ad8a5b5f595dd9e9446187b
Expr* lhs = E->getLHS();
if (strcmp(lhs->getStmtClassName(), "DeclRefExpr") == 0)
{
// get name and type for Expr *lhs from DeclRefExpr
const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs);
The usual way we write these last two lines is:
if (const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr(lhs))
no string comparison required
no double-check (the string check and then the check in the dyn_cast)
For more details on LLVM's casting machinery:
http://llvm.org/docs/ProgrammersManual.html#isa
const ValueDecl *valueDecl = declRefExpr->getDecl();
if (valueDecl)
Just a stylistic point related to my first (in case you're planning to
be a Clang/LLVM contributor): we usually collapse the variable
declaration into the condition.
Hi,
Actually, my question is more about getting type as string from Expr or QualType.
Say I have,
QualType declQT = valueDecl->getType();
or
QualType lhsQT = lhs->getType(); // lhs is on Expr* type
Now, I want to call QualType.getAsString(PrintingPolicy &Policy) and I need compiler instance or AST context to get printing policy, how to get compiler instance or AST context here?
//compilerInstance_ptr?
clang::ASTContext &context = compilerInstance_ptr->getASTContext();
std::cerr << "\ttype: " << declQT.getAsString(context.getPrintingPolicy()) << "\n";
Using Decl, I could do this like decl->getASTContext(); - but same I cannot do here!
I am overriding method bool VisitBinAssign(BinaryOperator *E);
Rajendra
Hi,
Actually, my question is more about getting type as string from Expr or
QualType.
Say I have,
QualType declQT = valueDecl->getType();
or
QualType lhsQT = lhs->getType(); // lhs is on Expr* type
Now, I want to call QualType.getAsString(PrintingPolicy &Policy) and I need
compiler instance or AST context to get printing policy, how to get compiler
instance or AST context here?
//compilerInstance_ptr?
clang::ASTContext &context = compilerInstance_ptr->getASTContext();
std::cerr << "\ttype: " <<
declQT.getAsString(context.getPrintingPolicy()) << "\n";
Using Decl, I could do this like decl->getASTContext(); - but same I cannot
do here!
Off-hand, the simplest way I could find to do this was to look at how
QualType's dump() member is implemented:
It simply create a PrintingPolicy directly with a default LangOptions
( http://clang.llvm.org/doxygen/classclang_1_1QualType.html#a83734a19d59252c9527473a32cb44a12
). This may suffice for your needs. Otherwise I assume there's some
way to get the current ASTContext in your visitor, or perhaps track it
separately.