[cfe-dev] Accessing preprocessed text

Benjamin Schindler schindler at virtamed.com
Mon Jul 28 03:01:59 PDT 2014


Hi

I'm writing a clang tool and one of the things I have to do is to dump all
of the attributes of functions. So for example, If I specify an attribute
__attribute__((myAttribute("arg1"))), I'd like to output myAttribute("arg1")
There is the printPretty function of clang::Attr, but this also appends the
__attribute__  (which I could chip away, but that's not a nice solution).
So what I tried is to go via the source, but here I stumble over the usage
of macros/preprocessor. What I tried is this:

clang::SourceRange range = attr->getRange();clang::SourceRange
expandedRange(sm.getExpansionLoc(range.getBegin()),
sm.getExpansionLoc(range.getEnd()));assert(expandedRange.isValid());std::string
text = clang::Lexer::getSourceText(clang::CharSourceRange::getTokenRange(expandedRange),
sm, clang::LangOptions(), 0);

But if the attribute is specified by means of a preprocessor macro, I
get the name of the macro and not the expanded text.

What is the best way of getting the macro text?

Thank you




-- 
Benjamin Schindler, PhD
Software Engineer, VirtaMed AG
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140728/edb3373f/attachment.html>


More information about the cfe-dev mailing list