sdgfsdh
sdgfsdh

Reputation: 37111

How do I extract the search paths for headers in the standard library in Clang?

I am using Clang as a library and I would like to extract the paths where the user's standard library headers are.

I can extract this information using the command-line tool. For example, on OSX:

clang -E -v -x c++ /dev/null -fsyntax-only

...
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/bin/../include/c++/v1
 /usr/local/include
 /usr/local/bin/../lib/clang/3.9.0/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
...

However, I would like to get it programatically. Something like:

// (just an example)
std::vector<std::string> searchPaths = clang::GetTypicalHeaderSearchPaths();

How does Clang expose this information?

Upvotes: 4

Views: 2257

Answers (2)

samvv
samvv

Reputation: 2134

If anyone else stumbles on this: with LLVM 3.9.1, it is not enough to merely create a compiler instance and read header search information from there. You have to create the preprocessor as well using CI.createPreprocessor(); that's the only place - at least in 3.9.1 - where ApplyHeaderSearchOptions is called, and invoking that function is what you want if you want to get the default header search directories.

After that, you can read the headers from HeaderSearch attained from CI.getPreprocessor().getHeaderSearchInfo() using HeaderSearch.system_dir_begin() and HeaderSearch.system_dir_end().

Thanks @sdgfsdh for the hint on how to get going.

Upvotes: 1

Anton Korobeynikov
Anton Korobeynikov

Reputation: 9324

Unfortunately, it's not that easy, because the set of header search paths depends on plenty of factors, including language, platform, command line options, configure-time settings, etc. Also, usually driver may add additional paths, etc.

There is HeaderSearch / InitHeaderSearch classes inside Frontend library that do the bulk of the work. You may want to look into clang::ApplyHeaderSearchOptions - basically allow it to fill a HeaderSearch instance and you could iterate over various header search paths.

Upvotes: 1

Related Questions