Getting the SourceLocation of the qualifier of a parameter

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting the SourceLocation of the qualifier of a parameter

Magnus Reftel
Hi all,

I'm trying to find the start of a parameter declaration, but only have
limited success. The function I'm looking at is defined similar to

void foo(const int* p)
{
}

and I'm using ASTConsumer::HandleTranslationUnit to trawl though the
AST looking for function definitions. When I find one I'm interested
in, I do

FunctionDecl* function=static_cast<FunctionDecl*>(decl);
VarDecl* parm=function->getParamDecl(0);
SourceLocation pos;
pos=parm->getSourceRange().getBegin();  // Nope, didn't work
cout << "Parameter declaration starts at column " <<
source_manager->getSpellingColumnNumber(pos) << endl;
pos=parm->getTypeSpecStartLoc();  // Nope, didn't work wither
cout << "Parameter declaration starts at column " <<
source_manager->getSpellingColumnNumber(pos) << endl;
pos=parm->getTypeSourceInfo()->getTypeLoc().getFullSourceRange().getBegin();
// What about this one?
cout << "Parameter declaration starts at column " <<
source_manager->getSpellingColumnNumber(pos) << endl;

In all cases I've tried, I get the position of the "int", not the
position of the "const". Is there a way of doing it that I have
missed, or is this an omission?

A simple test case showing this problem is attached.

Best Regards
Magnus Reftel

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

const_pos-1.zip (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Getting the SourceLocation of the qualifier of a parameter

Douglas Gregor

On Apr 15, 2010, at 9:16 AM, Magnus Reftel wrote:

> Hi all,
>
> I'm trying to find the start of a parameter declaration, but only have
> limited success. The function I'm looking at is defined similar to
>
> void foo(const int* p)
> {
> }
>
> and I'm using ASTConsumer::HandleTranslationUnit to trawl though the
> AST looking for function definitions. When I find one I'm interested
> in, I do
>
> FunctionDecl* function=static_cast<FunctionDecl*>(decl);
> VarDecl* parm=function->getParamDecl(0);
> SourceLocation pos;
> pos=parm->getSourceRange().getBegin();  // Nope, didn't work
> cout << "Parameter declaration starts at column " <<
> source_manager->getSpellingColumnNumber(pos) << endl;
> pos=parm->getTypeSpecStartLoc();  // Nope, didn't work wither
> cout << "Parameter declaration starts at column " <<
> source_manager->getSpellingColumnNumber(pos) << endl;
> pos=parm->getTypeSourceInfo()->getTypeLoc().getFullSourceRange().getBegin();
> // What about this one?
> cout << "Parameter declaration starts at column " <<
> source_manager->getSpellingColumnNumber(pos) << endl;
>
> In all cases I've tried, I get the position of the "int", not the
> position of the "const". Is there a way of doing it that I have
> missed, or is this an omission?

It's a Clang bug. getTypeSpecStartLoc() should return the location of the "const". Want to take a shot at fixing it?

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev