Repository navigation
pass compiler include directories to libclang as -isystem - #19565
Conversation
clang drops a -I directory that duplicates one of its own system directories, so the order the compiler reported wasn't kept. With an upstream libclang on macOS that left the compiler's builtin headers ahead of libc++, whose <cstddef> then failed on every parse.
Split the arguments out into systemIncludeArgs() and parse a fixture through libclang: two temporary header directories reproduce clang dropping a -I that duplicates a system directory.
gtritchie
left a comment
There was a problem hiding this comment.
The -I → -isystem change looks right to me; the remaining items are minor and mostly about the test (inline). Nothing here is blocking — the request-changes state is procedural.
One item not tied to a line: NEWS.md. The same -I loop ships in v2026.10.1 (CompilationDatabase.cpp:1267-1270 there), which also honors RSTUDIO_LIBCLANG_PATH, so per .claude/CLAUDE.md ("When fixing a bug or adding a new feature, add an entry to the appropriate section ... of NEWS.md") a ### Fixed entry seems due. Exposure is narrow (macOS with an upstream libclang via RSTUDIO_LIBCLANG_PATH), so if you consider it not user-facing, skipping it is reasonable.
| // directories too, as the standard library's directory is in practice; | ||
| // that is what gets a '-I' for the same directory dropped | ||
| std::vector<std::string> args = {"-nostdinc"}; | ||
| std::vector<std::string> includeArgs = systemIncludeArgs({std.getAbsolutePath(), builtin.getAbsolutePath()}); |
There was a problem hiding this comment.
This pins systemIncludeArgs() itself, and it does catch the regression (the same arguments through the clang driver pass with -isystem and fail with -I). But nothing checks that baseCompilationArgs() routes the discovered includes through it: putting the old -I loop back at CompilationDatabase.cpp:1603 would still pass. Covering that is probably not worth a harness, since baseCompilationArgs runs R and the compiler; flagging it so the gap is a deliberate choice.
There was a problem hiding this comment.
Agreed, and it's deliberate. baseCompilationArgs() needs R and a compiler, so the unit test stops at the helper. The test-cpp.R error-limit test is the end-to-end check, though it only catches this against an upstream libclang on macOS.
| CXTranslationUnit_None); | ||
|
|
||
| ASSERT_NE(tu, nullptr); | ||
| EXPECT_EQ(lib.getNumDiagnostics(tu), 0u); |
There was a problem hiding this comment.
On failure this prints 1 vs 0 with no hint of which #error fired, or whether a different libclang emitted something unrelated. lib.getDiagnostic / lib.formatDiagnostic are available, so streaming each formatted diagnostic into the EXPECT_EQ message would make a failure self-explanatory.
There was a problem hiding this comment.
Fixed in ebfa670: the formatted diagnostics are streamed into the assertion message. With the helper emitting -I it now prints probe.c:4:2: error: builtin stddef.h was found ahead of the standard library one.
| 0, | ||
| CXTranslationUnit_None); | ||
|
|
||
| ASSERT_NE(tu, nullptr); |
There was a problem hiding this comment.
If parseTranslationUnit returns null, ASSERT_NE returns before lib.disposeIndex(index) below, leaking the index. It only happens on an already-failing run, so minor; disposing the index before the assert (or a scope guard) would cover it.
There was a problem hiding this comment.
Fixed in ebfa670 with a BOOST_SCOPE_EXIT that disposes the translation unit and the index.
|
|
||
| // 'std' stands in for the C++ standard library, whose <stddef.h> wraps | ||
| // the one in 'builtin', the compiler's own headers | ||
| FilePath std = root_.completeChildPath("std"); |
There was a problem hiding this comment.
Nit: a local named std shadows the namespace name for the rest of the test. It compiles only because qualified lookup ignores variables before ::, and it reads oddly next to std::vector<std::string> on line 254. Maybe stdDir / builtinDir?
There was a problem hiding this comment.
Renamed to stdDir / builtinDir in ebfa670.
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
…test Also rename the 'std' and 'builtin' locals, which shadowed the namespace name.
|
Test items fixed in ebfa670. On NEWS: I've left it out. |
Intent
test-cpp.R's "diagnostics continue past clang's default error limit" fails on macOS when the session loads an upstream libclang (seen with Homebrew's libclang 23 viaRSTUDIO_LIBCLANG_PATH, while working on #19563): with-ferror-limit=5it sees 4 errors in the file, not 5. The fifth is an error in a system header that never reaches the user:It is there on every parse of a C++ file in that configuration, not only in the test.
Approach
The include directories reported by R's compiler were passed to libclang as
-I. clang drops a-Idirectory that is also one of its own system directories, so the reported order isn't kept. Here the SDK'sc++/v1was dropped as a duplicate, while the compiler's builtin header directory (CommandLineTools/usr/lib/clang/21/include), not being one of this libclang's directories, stayed as-Iand so came ahead of libc++. libc++ requires its own<stddef.h>to be found before the builtin one.With Apple's libclang the builtin directory is its own resource directory and is dropped too, which is why this doesn't show up in the default macOS setup.
These directories are now passed as
-isystem(built by a newsystemIncludeArgs()helper), which keeps them in the order the compiler reported. The compilation database version is bumped so cached package configurations are regenerated. Header completion already reads-isystemdirectories. The Windows branch is unchanged: it passes-nostdinc, so there are no default directories to collide with.Automated Tests
CompilationDatabaseTests.cppgainsCompileFlagsTest.SystemIncludeArgsAreSearchedInReportedOrder. It reproduces the reordering with two temporary header directories and no real compiler or SDK:std/stddef.hwrapsbuiltin/stddef.hwith#include_next, the arguments come from the newsystemIncludeArgs()helper, and a trailing-isystem stdstands in for libclang's own system directory. The test parses a probe file through libclang and expects no diagnostics. It is skipped when libclang isn't loaded.With Apple's libclang 21 (the default on macOS) it passes on this branch and fails when the helper emits
-I.The existing
test-cpp.Rerror-limit test, which is where this was first seen, only catches it against an upstream libclang on macOS:RSTUDIO_LIBCLANG_PATH)test-cpp.R:69failsAlso built and run on Linux, in an arm64 Ubuntu 22.04 container with g++ 11, R 4.6.1, and libclang 18.1.8 from apt.llvm.org. This change was applied there on top of #19563 rather than built on its own:
rsessiongteststest-cpp.R-IIn a standalone libclang harness on Ubuntu 24.04 x86_64 (g++ 13, R 4.6.1, libclang 18 and 20), parsing an Rcpp file with the generated arguments reports no diagnostics with either
-Ior-isystem, so the change doesn't alter what Linux users see.This PR and #19563 edit adjacent lines in
CompilationDatabase.cpp, so whichever merges second needs a trivial conflict resolved.QA Notes
None; C/C++ completion and diagnostics should behave as before.
Documentation
None needed.
Checklist
NEWS.md