I am trying to using clang to do autocomplete, How to go to declaration of a macro?

For exmaple

#define FOO 0

int foo() {
int x = FOO; /* How to go to decleration of FOO here? *./
}

I’m not sure that I understood your question correctly, but if you are working on auto completion then you can use Preprocessor::getMacroDefinition to lookup the definition of a some macro. Specifically, as long as you’re working with Sema, you can use code similar to the one below:

for (const auto &M : PP.macros()) {
auto MD = PP.getMacroDefinition(M->first);
if (MD) {
// you have access to macro definition here and can lookup it’s source location
// by using MD.getMacroInfo()->getDefinitionLoc()
}
}

Alex

Thanks a lot, Alex.
I am trying to do that in command line:) how to do that?I meas clang.exe driver:)

Thanks a lot, Alex.
I am trying to do that in command line:) how to do that?
I meas clang.exe driver:)

As far as I know you can't query about the definition of a macro with
command line and clang, but you can list all the macro definitions by using
"clang -dM -E foo.cpp". This might work for you if you're just interested
in the text of the macro definition, since you can search for a macro
like "FOO"
in that list.

I am interested in the location of that macro:( because I want to implement go to definition directly by calling to clang.exe.

I don't think that's possible. You either need to find a different tool that builds upon libclang or implement a tool yourself that uses libclang [1]. The libraries expose this functionality but not the compiler itself, as far as I know.

[1] http://clang.llvm.org/doxygen/group__CINDEX__CODE__COMPLET.html

罗勇刚(Yonggang> Thanks a lot, Alex. I am trying to do that in
    罗勇刚(Yonggang> command line:) how to do that? I meas clang.exe
    罗勇刚(Yonggang> driver:)

Have you looked at some applications based on Clang and doing already
similar things, such as RTags https://github.com/Andersbakken/rtags ?