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

Kim Gräsman kim.grasman at gmail.com
Tue May 19 13:15:23 PDT 2015


On Fri, May 15, 2015 at 8:59 AM, Kim Gräsman <kim.grasman at gmail.com> wrote:
> On Tue, May 12, 2015 at 4:39 AM, Richard Smith <richard at metafoo.co.uk> wrote:
>>
>> 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 SourceManager.
>>
>> 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
>> history.
>
> I honestly can't remember exactly what I want anymore :-), it's been a
> few months since I worked on the problematic case, but the
> PreprocessingRecord looks interesting.

After some recent hacking, I now know what I'm looking for:

I have a Decl and a location where it's used.

If that location is in a macro, we want to attribute the use to the
file containing the macro, unless
  a) there is a prior forward-declaration of the Decl in the macro file
  b) the use-location points to a macro-argument

So for example:

--
// macro.h

// attribute std::max to macro.h, because it's an implementation
// detail of YET_ANOTHER_MAX.
#include <algorithm>
#define YET_ANOTHER_MAX(x, y)  std::max(x, y)

// attribute Thing to callers because it's forward-declared,
// and we use that as a marker that we want to defer responsibility.
class Thing;
#define DECLARE_THING(name) Thing name;

// attribute x to callers, because it's a macro argument,
// and they're responsible for what they pass in.
#define LENGTH_OF(x) (sizeof(x) / sizeof(*x))
--

The last case is what I'm having trouble with; starting from a
location known to be a macroID, can I find out if it maps back to a
macro argument in the same macro?

Thanks,
- Kim




More information about the cfe-dev mailing list