fix(lint): look for libc++ where the compiler does

In crafter-build's Flatpak, whose compiler keeps libc++ in /app and
whose /usr has none, a file that includes a header of the C++ standard
library could not be parsed for the lint rules that read its AST in a
build like that of #46: the file built, and `crafter-build lint` then
ran none of those rules on it, printed "'algorithm' file not found"
and exited 1.

libclang parses through a clang driver of its own. In a build for the
host whose compile command names no libc++ directory, as #46's names
none, where a sysroot or CRAFTER_BUILD_LIBCXX_DIR would name one, that
driver looks for libc++'s headers under ../include from the directory
of its argv[0], then under /usr/local/include and /usr/include.
ParseAst gave libclang the compile command without its compiler, and
clang_parseTranslationUnit names argv[0] plain "clang", in no
directory. So the parse of such a build, natively as in the Flatpak,
took the first libc++ it found under ../include from the working
directory, /usr/local/include and /usr/include, whichever compiler the
build used. The -resource-dir ParseAst passes is for clang's own
headers and changes nothing for libc++.

The compile command's argv[0] would not do on its own: a clang++ from
PATH is the bare name clang++, in no directory either, and a clang++
reached through a link into another directory takes itself to be where
the link leads, as clang finds its installation from the file it runs
as. The compiler says where that is, on the InstalledDir line of
`clang++ --version`, which GetToolchain() already runs for clangVersion.
Toolchain::installedDir keeps it, InstalledClangxx(command) names the
compiler in it, and ParseAst passes that as argv[0], through
clang_parseTranslationUnit2FullArgv, which takes argv[0] from its
caller. With argv[0] there, the parse finds libc++ where the compile
does, whatever the working directory.

A compile command can have the compiler look from elsewhere: with
-no-canonical-prefixes, where the last of it and -canonical-prefixes is
that one, clang takes itself to be at clangxx as written, or, where no
file is there, as is usual for a bare name, at the one PATH gives, a
link not followed, and `clang++ -no-canonical-prefixes --version` names
that directory. GetToolchain() runs that too,
Toolchain::installedDirAsWritten keeps what it names, and
InstalledClangxx(command) names the compiler there for a command whose
last prefix flag is -no-canonical-prefixes, as clang's own driver reads
them.

argv[0] also gives libclang's driver its mode, by its file name, and
the configuration files it reads by default, from its directory. The
parse now runs in the g++ mode the compile runs in, where it ran in
clang's gcc mode, and reads the default configuration files beside the
compiler that the compile reads, clang++.cfg among them; before, it read
none from there, as the driver skips the directory of an argv[0] that
has none.

-resource-dir stays, for a libclang of another release than the
compiler, whose driver would derive the directory from argv[0] for its
own release. The comment on it said libclang found clang's own headers
relative to its own install path; its driver derived them from argv[0],
as it does libc++'s.

Fixes #46
This commit is contained in:
Jorijn van der Graaf 2026-11-27 16:16:38 +01:00
commit 8019f9d44f
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 87 additions and 8 deletions

View file

@ -43,6 +43,7 @@ namespace {
decltype(&clang_createIndex) createIndex = nullptr;
decltype(&clang_disposeIndex) disposeIndex = nullptr;
decltype(&clang_parseTranslationUnit) parseTranslationUnit = nullptr;
decltype(&clang_parseTranslationUnit2FullArgv) parseTranslationUnit2FullArgv = nullptr;
decltype(&clang_disposeTranslationUnit) disposeTranslationUnit = nullptr;
decltype(&clang_getFile) getFile = nullptr;
decltype(&clang_getLocationForOffset) getLocationForOffset = nullptr;
@ -133,6 +134,7 @@ namespace {
bind(lib.createIndex, "clang_createIndex");
bind(lib.disposeIndex, "clang_disposeIndex");
bind(lib.parseTranslationUnit, "clang_parseTranslationUnit");
bind(lib.parseTranslationUnit2FullArgv, "clang_parseTranslationUnit2FullArgv");
bind(lib.disposeTranslationUnit, "clang_disposeTranslationUnit");
bind(lib.getFile, "clang_getFile");
bind(lib.getLocationForOffset, "clang_getLocationForOffset");
@ -705,11 +707,12 @@ namespace {
return decls;
}
// libclang locates its builtin headers relative to its own install path,
// which need not agree with the clang++ on PATH that produced the PCMs.
// When it disagrees the failure is total and unhelpful — every parse dies
// on "'stddef.h' file not found" — so ask the driver and pass it
// explicitly. Cached like HostTarget, and for the same reason.
// The resource directory, which holds clang's own headers such as
// stddef.h, as the compiler names it. libclang's driver would otherwise
// derive one from argv[0] for libclang's own release, so with a libclang
// of another release than the compiler, a file that includes a header of
// the C++ standard library would fail on "'stddef.h' file not found".
// Cached like HostTarget, and for the same reason.
const std::string& ClangResourceDir() {
static const std::string Cached = []() -> std::string {
CommandResult r = RunCommandChecked(std::format("{} -print-resource-dir", GetToolchain().clangxx.string()));
@ -752,7 +755,26 @@ namespace {
// Byte-length preserving, so cursor line/column land on the original.
std::string buffer = BlankExportKeywords(content, tokens);
std::string path = file.string();
std::vector<std::string> args = CommandToArgs(compileCommand);
// In a build for the host whose compile command names no libc++
// directory (a sysroot or CRAFTER_BUILD_LIBCXX_DIR names one), the
// driver inside libclang looks for libc++'s headers in ../include from
// the directory of argv[0], as the compiler's driver does from its
// own, and then in /usr/local/include and in /usr/include. Left to
// name argv[0] itself, libclang names it plain "clang", in no
// directory, so the first of those was ../include from the working
// directory, and where none of them had a libc++, as in the Flatpak,
// whose compiler keeps its own in /app, no file of such a build that
// includes a header of the C++ standard library could be parsed
// (issue #46). The compile command's own argv[0] would not do either:
// a clang++ from PATH is a bare name, and one reached through a link
// into another directory takes itself to be where the link leads,
// unless the command's last prefix flag is -no-canonical-prefixes. So
// argv[0] is the compiler where clang started with this command takes
// itself to be, in the directory it says it is installed in when run
// with the command's last prefix flag, or, where it names none,
// clangxx as the command has it (Toolchain::InstalledClangxx).
std::vector<std::string> args = { GetToolchain().InstalledClangxx(compileCommand).string() };
std::ranges::move(CommandToArgs(compileCommand), std::back_inserter(args));
// Required: libclang will not infer a module interface unit from the
// .cppm extension, and silently treats every flag as a linker input if
// left to guess.
@ -772,8 +794,8 @@ namespace {
result.error = "clang_createIndex failed";
return result;
}
CXTranslationUnit tu = lc.parseTranslationUnit(index, path.c_str(), argv.data(), static_cast<std::int32_t>(argv.size()), &unsaved, 1, CXTranslationUnit_None);
if (!tu) {
CXTranslationUnit tu = nullptr;
if (lc.parseTranslationUnit2FullArgv(index, path.c_str(), argv.data(), static_cast<std::int32_t>(argv.size()), &unsaved, 1, CXTranslationUnit_None, &tu) != CXError_Success || !tu) {
lc.disposeIndex(index);
result.error = "clang could not create a translation unit";
return result;