[cfe-dev] Printing Source Information within Visit*

Douglas Gregor dgregor at apple.com
Mon Sep 26 07:23:20 PDT 2011


On Sep 23, 2011, at 6:32 AM, Mark Fenner wrote:

> 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



More information about the cfe-dev mailing list