Accessing some particular source locations in the AST

Hi guys. I’m trying to get a SourceLocation for a couple of points in the source. So for:

class Foo****: public Bar {

Starting from a CXXRecordDecl (which I get from RecursiveASTVisitor::VisitCXXRecordDecl()), currently I am only able to get the location before the class keyword (with TagDecl::getInnerLocStart()) and after it (TagDecl::getOuterLocStart()).

I’ve been struggling with this for a while now, and the documentation really isn’t helping. There are many missing enums and member functions in the docs. Am I supposed to use the lexer somehow? How? Am I supposed to use VisitTypeLoc()? I dunno, help!

This is for a source translation tool that translates a small subset of C++ to JS. So:

class Foo: public Bar {
Foo(int x, float y) {


function Foo(x, y) {

// Do some inheritance stuff for Bar

Why do you need those particular source locations to perform this task,
though? Sounds like all you need is find the class name, its base(s) and
constructor(s) - all that is readily available in the AST.


I need those locations in order to retain comments and indentation by simply doing a text replace instead of rebuilding the text.

Hi guys. I'm trying to get a SourceLocation for a couple of points in the
source. So for:

class Foo*<HERE>*:

Lexer::getLocForEndOfToken(RD->getLocation(), 0, /*...*/)

public Bar *<AND HERE>*{

Do you want the location of the open brace, or the location after each base
specifier? For the base specifiers, iterate over RD->bases() and grab the
source range. (Use getLocForEndOfToken on the end of the range.) We don't
directly store the location of the left brace, but you can get an
approximation of the right thing by scanning forwards through the input
buffer looking for it (you can be defeated by macros, but that's hard to
avoid without re-lexing).

Starting from a CXXRecordDecl (which I get from

Thanks. Both worked. Also, aren’t macros already expanded at this point?