[cfe-dev] Printing Source Information within Visit*

Mark Fenner mark.fenner at coveros.com
Fri Sep 23 06:32:40 PDT 2011


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



More information about the cfe-dev mailing list