TypeAliasDecl SourceRange is not giving proper range

I am new to clang ast, and trying to parse some c++ code with it.

When I am using TypedefDecl->getSourceRange() for saytypedef long double LDBL it returns the whole thing
However when I am using TypeAliasDecl->getSourceRange() for say
using LDBL = long double
it only returns the range using LDBL = long. (i.e removes the double part)

I used the code below to extract text from SourceRange

char const* first = sm.getCharacterData(r.getBegin());
char const* last = sm.getCharacterData(pp.getLocForEndOfToken(r.getEnd()));
return std::string(first,last);

Am I doing something wrong? How can I get the whole source range, and esp the type part of it i.e. long double part, so that I can rearrange it as typedef long double LDBL

I am using clang 3.2 with RAV

Any help will be appreciated.
Thanks

Who would I talk to about getting the build bots to track clang-tools-extra?

Sorry, didn’t mean to reply to this thread.

Hi!

I am new to clang ast, and trying to parse some c++ code with it.

When I am using TypedefDecl->getSourceRange() for saytypedef long double LDBL it returns the whole thing
However when I am using TypeAliasDecl->getSourceRange() for say
using LDBL = long double
it only returns the range using LDBL = long. (i.e removes the double part)

I used the code below to extract text from SourceRange

char const* first = sm.getCharacterData(r.getBegin());
char const* last = sm.getCharacterData(pp.getLocForEndOfToken(r.getEnd()));
return std::string(first,last);

The problem here is that BuiltinTypeLoc doesn’t store the source locations for every keyword in the type, just for (apparently) the first one. Consequently, BuiltinTypeLoc::getLocalSourceRange() always returns a one-keyword range, and TypeAliasDecl::getSourceRange() picks the end of that range as the end of the declaration.

Am I doing something wrong? How can I get the whole source range, and esp the type part of it i.e. long double part, so that I can rearrange it as typedef long double LDBL

I am using clang 3.2 with RAV

We should fix BuiltinTypeLoc to track the end location in the needsExtraLocalData() case. If you want a workaround for this, you could re-lex tokens from the end location until you hit a semicolon (this is imperfect in the presence of macros, but your code isn’t robust against macro expansions within the alias declaration anyway, so this may be enough for your purposes) – or even just scan through the character data until you hit a semicolon if you’re not concerned about comments at the end of the declaration.

Thanks for the explanation.
I fixed it by traversing until a semicolon,and getting the full type as
text. I guess I can fix it using needsExtraLocalData, like the
ASTWriter.cpp does.
I have a second problem. Given a TypeLoc, how can I find whether it is in
base class specifier list?
e.g.
   struct x{};
   struct y: x{};
   void foo()
{
   x a;
}
I can do so by traversing RecordDecl, but I like to do it in reverse way,
as I want to rewrite that case differently from all other cases.

Thanks.