[cfe-dev] Macro expansion with Clang

James Dennett james.dennett at gmail.com
Tue Oct 15 16:26:19 PDT 2013


On Tue, Oct 15, 2013 at 4:09 PM, Edward Diener
<eldlistmailingz at tropicsoft.com> wrote:
> On 10/15/2013 2:13 PM, James Dennett wrote:
>>
>> On Mon, Oct 14, 2013 at 9:48 PM, Alexander Basov <coopht at gmail.com> wrote:
>>>
>>> Hello.
>>> Could somebody help me with macro expansion?
>>>
>>> I have a header with the following macroses defined:
>>>
>>> #define foo(x) x + 2
>>> #define A 1
>>> #define B foo(A)
>>>
>>> And I need get the following map:
>>> A = 1
>>> B = 3
>>>
>>> How can I do it?
>>>
>>> I'm ale to get list of MacroInfo * classes with PPCallbacks or get it
>>> with
>>> Preprocessor::macro_begin() iterator, but I don't know how to get
>>> expaneded
>>> value.
>>
>>
>> I think you may be looking in the wrong places, or at least not in all
>> of the right places.
>>
>> The expanded value of "B" is "A + 2", not "3".  Macro expansion
>> doesn't evaluate arithmeic.
>
>
> That is not correct. Invoking B gives "1 + 2".

Sorry, indeed; that was a typo.  My brain fully expanded the macro,
but my fingers somehow stopped after one level.  Fortunately I don't
think it's material.

The point still stands: if you want the value, not the expansion, you
need to do evaluation.

-- James



More information about the cfe-dev mailing list