Macro expansion with Clang

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.

Thank you in advance.

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.

If you want to get the value 3 for "B", you need to expand and then
evaluate the preprocessor constant expression "B", as is done in
processing the #if directive.

-- James

In article <CAKNGpgr5Djj-Dvrvp=h5ubkSjF2Bz_PAi0+_+oAbk-4HJHNObg@mail.gmail.com>,
    James Dennett <james.dennett@gmail.com> writes:

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?

One way:

#include "your_header.h"

int main() {
  printf("A = %d\nB = %d\n", A, B);
  return 0;
}

What additional constraints do you have? Do you need to do this using the
Clang C++ API for some reason? Do you know in advance the names of the
macros that you want to evaluate?

I'm ale to get list of MacroInfo * classes with PPCallbacks or get it with

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

That is not proper way for me.
I cannot include the header and compile source and I don’t know macro names that i want to evaluate.
There is no any reason to use C++ API, but if you know how to do it with libclang-c that’s could be great.

Could you point me out to some example how can I do that?