Printing Source Information within Visit*

Hello,

Within a VisitIfStmt, I'd like to print out some information that is
primarily for my own debugging purposes as I develop the functionality
of a VisitIfStmt. In
particular, I'd like to print out the conditional expression of the
"if" more or less as it appeared in the source code. Is there, maybe,
a built-in way to
unparse or collapse the AST of the conditional to a simple string
representation with extra parentheses. Doing a dumpAll(S->getCond())
is a bit too much.

Is there a simple way to do this? When I started trying to print
source code I got bogged down in -- I think -- PrinterHelper.

I can get line numbers (by giving the IfStmtConsumer a pointer to a
SourceManager -- is there something more direct? Like retrieving the
SourceManager
from a parent class of IfStmtConsumer).

class IfStmtConsumer :
     public clang::ASTConsumer,
     public clang::RecursiveASTVisitor<IfStmtConsumer> {

  bool VisitIfStmt(IfStmt *S){

      /*
          print out some debugging information about S->getCond() like
the original source code (more or less)
          line number, etc.
      */

      /* do some processing on S */

  }

  virtual void HandleTranslationUnit(clang::ASTContext &context) {
    clang::TranslationUnitDecl* tu_decl = context.getTranslationUnitDecl();
    TraverseDecl(tu_decl);
  }
};

Thanks,
Mark

Hello,

Within a VisitIfStmt, I'd like to print out some information that is
primarily for my own debugging purposes as I develop the functionality
of a VisitIfStmt. In
particular, I'd like to print out the conditional expression of the
"if" more or less as it appeared in the source code. Is there, maybe,
a built-in way to
unparse or collapse the AST of the conditional to a simple string
representation with extra parentheses. Doing a dumpAll(S->getCond())
is a bit too much.

dump/dumpAll are meant for debugging.

Is there a simple way to do this? When I started trying to print
source code I got bogged down in -- I think -- PrinterHelper.

The Stmt class (from which Expr derives) has a printPretty member function that will try to parse the source as it was parsed.

I can get line numbers (by giving the IfStmtConsumer a pointer to a
SourceManager -- is there something more direct? Like retrieving the
SourceManager
from a parent class of IfStmtConsumer).

There's no way to get a SourceManager from a parent class. Note that once you have a reference to the source manager, you also have the option to go back to the original source code (using the source range provided by Stmt::getSourceRange()) to retrieve the original text.

  - Doug