[cfe-dev] SourceLocation -> MacroDefinition(Record?)

Richard Smith richard at metafoo.co.uk
Mon May 11 19:39:19 PDT 2015

On Mon, May 11, 2015 at 12:15 PM, Kim Gräsman <kim.grasman at gmail.com> wrote:

> Hi all,
> For IWYU, we can usually make better suggestions if we know whether a
> use happens inside a macro or not.
> I should be able to use SourceLocation::isMacroID to ask whether I'm
> in *a* macro, but is there a way to go from there to *which* macro?
> I'm guessing that's represented by the new MacroDefinition class.
> I've toyed with the idea of recording all macro definitions in a
> PPCallbacks implementation and building a reverse lookup table from
> location to macro definition, but I figured Clang might already have
> that. Does it?

Yes, but it depends on what you want. You can ask the SourceManager for
information about a macro SourceLocation to find where the macro was
defined and where it was expanded. In common cases you can get the name of
the macro this way, but if the name of the macro was itself produced by
macro expansion, it's tricky to extract that information from the

If you want more information, one option would be to use
clang::PreprocessingRecord. This is a prebuilt implementation of what you
mention above: a PPCallbacks implementation that tracks the macro expansion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150511/073d16b5/attachment.html>

More information about the cfe-dev mailing list