[cfe-dev] source-code representation of an Expr
Sam
sam at cs.jhu.edu
Tue Jan 4 13:10:41 PST 2011
What is the right way to address the first condition in the code you
suggested? I have been banging my head against my monitor long enough that
I now admit defeat. I've been looking mostly at the APIs for
clang::Preprocessor and clang::SourceManager. I've also read through the
Clang Internals document.
The bigger issue is that I'd really just like to understand how to properly
traverse the source-code representation of statements I encounter via the
AST in an efficient manner. Ideally this would include the ability to
examine the source-code representation of a statement (or one of its
sublcasses) before or after preprocessing, e.g., before and after macro
expansion.
Here's what I know so far:
1. For an expression "Expr * e", I can retrieve its source range via
e->getSourceRange().
2. Using SourceRange::getBegin() I can call
SourceManager::getCharacterData()
3. From what I can tell, SourceManager::getCharacterData returns source-code
post macro-expansion.
4. If my expression doesn't contain any macros, the code that John McCall
suggested works fine for my purposes. However, I am interested in a number
of cases where there are macros involved (specifically, I am looking at
function arguments, i.e., my "expressions", that contain macros).
Can anyone help? I feel that Clang is likely capable of providing me with
the information that I want as-is, but I just can't figure this one out via
API intuition. If it's not, I'd be glad to submit a patch to the API if
anyone can steer me in the right direction.
Thanks in advance,
Sam
On Wed, Dec 29, 2010 at 4:20 PM, John McCall <rjmccall at apple.com> wrote:
> On Dec 29, 2010, at 12:53 PM, Sam wrote:
> > Yes, I just don't know what to *do* with it ;-) In other words, I don't
> see a clear use of the SourceManager API once I have the SourceRange to
> extract the Expr's source-code. I don't see any API calls that use
> SourceRange or beginning and ending SourceLocations for source-code
> extraction.
>
> We should probably make some API for this.
>
> What you can do for now is something like the following:
>
> SourceRange range = expr->getSourceRange();
> if (range.getBegin().isMacroID() || range.getEnd().isMacroID()) {
> // handle this case
> } else if (!sourceManager.isFromSameFile(range.getBegin(),
> range.getEnd())) {
> // handle this case
> } else {
> range.setEnd(preprocessor.getLocForEndOfToken(range.getEnd()));
> const char *begin = sourceManager.getCharacterData(range.getBegin());
> const char *end = sourceManager.getCharacterData(range.getEnd());
> llvm::StringRef string(begin, end - begin);
> // now you can do whatever you want
> }
>
> John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110104/f32f1c43/attachment.html>
More information about the cfe-dev
mailing list