1

I am relatively new to CLang and Libtooling. I want to display a Line from the source code on the terminal. I have a *VisitFunctionDecl(FunctionDecl func) in the RecursiveASTVisitor. For every function I get the SourceRange and from that the SourceLocation. But I dont understand how to display that. I has something to do

Awaid Shaheen
  • 95
  • 1
  • 9
  • Can't you use [`SourceLocation::print`](http://clang.llvm.org/doxygen/classclang_1_1SourceLocation.html#ae4ea241af69a871313e928909db6633d) or [`SourceLocation::printToString`](http://clang.llvm.org/doxygen/classclang_1_1SourceLocation.html#a700ac4324e055d2d96e9764f4d507103)? – Some programmer dude Jul 07 '15 at 11:43
  • I need SourceManager and I dont know how to access/declare that. – Awaid Shaheen Jul 07 '15 at 11:55
  • Don't you have an [`ASTContext`](http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html) pointer? It can give you a `SourceManager` reference. Or give you a [`FullSourceLoc`](http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html) instance which you can use directly to get the source line (if I read the reference correctly). Maybe you should read e.g. [this `RecursiveASTVisitor` tutorial](http://clang.llvm.org/docs/RAVFrontendAction.html)?' – Some programmer dude Jul 07 '15 at 12:05
  • Thank you very much. As you said I used the ASTContext to get the FllSourceLoc and using that I got what I was lookinf for. – Awaid Shaheen Jul 07 '15 at 12:35

1 Answers1

0

You need the FullSourceLoc:

    FullSourceLoc functionDeclFullLocation = Context->getFullLoc(func.getLocStart());
    if (functionDeclFullLocation.isValid())
        llvm::outs() << "Found FunctionDecl at "
        << functionDeclFullLocation.getManager().getFilename(functionDeclFullLocation) << ":"
        << functionDeclFullLocation.getSpellingLineNumber() << ":"
        << functionDeclFullLocation.getSpellingColumnNumber() << "\n";
andi475
  • 69
  • 3