Benno
Benno

Reputation: 5456

How to print source location from a clang AST Node

I'm loosely following the tutorial at http://clang.llvm.org/docs/LibASTMatchersTutorial.html . I managed to create an AST Matcher that matches class definitions and my MatchFinder looks like this

class ClassDeclPrinter : public MatchFinder::MatchCallback
{
public:
  virtual void run(const MatchFinder::MatchResult &result) override
  {
    if (clang::NamedDecl const* nd = result.Nodes.getNodeAs<clang::NamedDecl>("id"))
    {
      nd->dump();
    }
  }
};

If I call dump() on the matched node, the output already contains its file and source location:

CXXRecordDecl 0x10dd250 </home/name/llvm-dev/build/../../cpp/classes.cpp:4:1, line:6:1> class B definition

However, when I call getLocation(), I only get a SourceLocation object, whose print() and dump() functions need a SourceManager instance to work. I have no idea how to obtain this SourceManager, or if there is another way to get a printable source location.

Upvotes: 8

Views: 2852

Answers (1)

Benno
Benno

Reputation: 5456

The correct SourceManager is available via

result.Context->getSourceManager();

Upvotes: 8

Related Questions