[cfe-dev] Getting the SourceLocation of the qualifier of a parameter

Douglas Gregor dgregor at apple.com
Thu Apr 15 09:55:02 PDT 2010


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



More information about the cfe-dev mailing list