Skip to content

Instantly share code, notes, and snippets.

@PlloYNiiE
Created March 17, 2011 16:33
Show Gist options
  • Select an option

  • Save PlloYNiiE/874631 to your computer and use it in GitHub Desktop.

Select an option

Save PlloYNiiE/874631 to your computer and use it in GitHub Desktop.
HandleTopLevelDecl.cpp
void MyASTConsumer::HandleTopLevelDecl( clang::DeclGroupRef d)
{
llvm::SmallVector<Decl*, 2> Decls;
static int count = 0;
clang::DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++)
{
count++;
// find VarDecl
clang::VarDecl *vd = dyn_cast<clang::VarDecl>(*it);
if(vd)
{
std::cout << "\nAST" << id << "---> is VD";
std::cout << "\nDecl name: \t" << vd->getDeclName().getAsString() << std::endl;
objCollection->VarDecl.push_back( vd->getDeclName().getAsString());
//cout << "PUSH SIZE " << objCollection->VarDecl.size() << endl;
//(objCollection->VarDecl).assign(vd->getDeclName().getAsString());
if( vd->isFileVarDecl() && vd->hasExternalStorage() )
{
std::cerr << "\nAST" << id << "---> Read top-level variable decl: '";
std::cerr << vd->getDeclName().getAsString() << "'";
std::cerr << std::endl;
}
else {
std::cout << "type >> \t" << vd->getType().getAsString() << std::endl;
Expr *InitialExpr = vd->getInit();
//dyn_cast<IntegerLiteral>(InitialExpr);
if(IntegerLiteral *IL = dyn_cast<IntegerLiteral>(InitialExpr))
{
std::cout << "integer value: \t" << IL->getValue().getSExtValue();
}
if(FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InitialExpr))
{
std::cout << "float value: \t" << FL->getValue().convertToDouble();
}
//std::cout << "value: " << vd->getInit() ;
//vd->getDeclContext()->dumpDeclContext();
}
}
else if(!vd)
{
// std::cout << "AST" << id << "---> not varDecl\n";
clang::FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
if (!fd)
{
continue;
}
if (fd->isFunctionOrFunctionTemplate())
{
std::cout << "AST" << id << "---> is Function or Fn Template\n";
std::cout << "fn Name: "<<fd->getDeclName().getAsString() << std::endl;
// fd->dump();
Stmt *S = fd->getBody();
SS::SieveStatementPrinter ssp;
ssp.VisitSubTree(S);
Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
for( ; CI != CE; CI++)
{
Stmt *ChildStmt = *CI;
//ssp.VisitSubTree(ChildStmt);
// ChildStmt->dump();
}
}
else
std::cout << "AST" << id << "---> is not FN\n";
continue;
}
}//end for
} //HandleTopLevelDecl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment