Description
I can’t seem to figure out how to get Clang to add C++ system include directories to the include path. I’m not sure this really counts as an issue (I’ve also already tried asking in the #libtooling
channel on the LLVM Discord server, and so far, we haven’t been able to find an answer to this).
Essentially, I want to take some C++ code that includes C++ stdlib headers (e.g. #include <cstdlib>
), parse it, and then do something with the resulting ASTContext
. I started out trying to run a frontend action on it via runToolOnCode
, but that doesn’t seem to add any include directories.
Next, I tried using clang::ASTUnit::LoadFromCompilerInvocation
to create an ASTContext
from a CompilerInvocation
, which I managed to obtain in one of three ways:
- Calling
clang::CompilerInvocation::CreateFromArgs
, - Creating a
clang::driver::Driver
, using that to create aCompilation
, extracting the arguments from that compilation, callingAddClangCXXStdlibIncludeArgs
on the tool chain that that compilation gives me, presumably to add the include paths that I’m looking for, and finally passing all of those arguments toclang::CompilerInvocation::CreateFromArgs
, - Calling
clang::createInvocation
.
So far, while the latter two approaches do manage to pull in some system headers (e.g. it manages to find stdlib.h
), it still can’t seem to find any C++ system headers (e.g. <cstdlib>
).
It would be helpful to know what the ‘proper’ way of accomplishing any of this is; I can, of course, add the include paths manually as command-line arguments to any of the three approaches mentioned above, but doing so 1. becomes rather untenable once different platforms are involved, and 2. seems rather janky seeing as Clang knows what the right include directories are (after all, it never fails to find them when I try and run clang foo.cc
from the terminal), and it seems like a bad idea to try and do that manually when some part of Clang already knows how to do that.
So, to summarise, all I would like to be able to do is take some C++ code as a string and get an ASTContext
from it that I can work with, and I want clang to behave in pretty much exactly the same manner as though I had run it from the terminal.
Not sure how much this helps, but I’m running Fedora 39.