[cfe-dev] cfe-dev Digest, Vol 67, Issue 3
Rajendra
rks at cse.iitb.ac.in
Tue Jan 1 20:44:23 PST 2013
> Message: 2
> Date: Tue, 1 Jan 2013 22:10:30 +0800
> From: "=?gb18030?B?a2V2aW5seW54?=" <kevinlynx at gmail.com>
> To: "=?gb18030?B?Tmlrb2xhIFNtaWxqYW5pYw==?=" <popizdeh at gmail.com>
> Cc: cfe-dev <cfe-dev at cs.uiuc.edu>
> Subject: Re: [cfe-dev] how to determine a parameter has a type
> declarator in clang
> Message-ID: <tencent_3B0F185D36A7D35B1138CC5C at qq.com>
> Content-Type: text/plain; charset="gb18030"
>
> Thanks for your reply. But i still can not figure it out. I get
> function declaration by these codes below:
>
> class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
> {
> public:
> bool VisitFunctionDecl(FunctionDecl *f) {
> for (FunctionDecl::param_iterator it = f->param_begin(); it
> != f->param_end(); ++it) {
> ParmVarDecl *p = *it;
> ...
> }
> return true;
> }
> };
>
> which means I only get `FunctionDecl` object. I do not know how to
> get a `FunctionTypeInfo` you mentioned. Maybe I 'm not on the right
> way to parse a function definition/declaration.
>
> BTW, I'm writing a static code analyzer tool, and i will check
> whether a function parameter has a type declarator.
>
Try something like this:
// Get function parameters
std::cerr << "\n Function Parameters: \n";
for (clang::FunctionDecl::param_iterator pit =
functionDecl->param_begin(); pit != functionDecl->param_end();
pit++)
{
clang::Decl *param = *pit;
const NamedDecl *namedDecl = dyn_cast<NamedDecl>(param);
if (namedDecl)
{
std::cerr << "\tparam name = " << namedDecl->getNameAsString() <<
"\n";
}
const ValueDecl *valueDecl = dyn_cast<ValueDecl>(param);
if (valueDecl)
{
QualType declQT = valueDecl->getType();
clang::ASTContext &context = param->getASTContext();
std::cerr << "\ttype = " <<
declQT.getAsString(context.getPrintingPolicy()) << "\n";
}
- Rajendra
More information about the cfe-dev
mailing list