Skip to content

pass compiler include directories to libclang as -isystem - #19565

Merged
kevinushey merged 3 commits into
mainfrom
bugfix/libclang-system-include-order
Nov 11, 2026
Merged

kevinushey merged 3 commits into
mainfrom
bugfix/libclang-system-include-order

Conversation

@kevinushey

@kevinushey kevinushey commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

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 via RSTUDIO_LIBCLANG_PATH, while working on #19563): with -ferror-limit=5 it sees 4 errors in the file, not 5. The fifth is an error in a system header that never reaches the user:

.../MacOSX.sdk/usr/include/c++/v1/cstddef:45:5: error: <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header.

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 -I directory that is also one of its own system directories, so the reported order isn't kept. Here the SDK's c++/v1 was 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 -I and 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 new systemIncludeArgs() 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 -isystem directories. The Windows branch is unchanged: it passes -nostdinc, so there are no default directories to collide with.

Automated Tests

CompilationDatabaseTests.cpp gains CompileFlagsTest.SystemIncludeArgsAreSearchedInReportedOrder. It reproduces the reordering with two temporary header directories and no real compiler or SDK: std/stddef.h wraps builtin/stddef.h with #include_next, the arguments come from the new systemIncludeArgs() helper, and a trailing -isystem std stands 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.R error-limit test, which is where this was first seen, only catches it against an upstream libclang on macOS:

libclang before after
Apple 21 (default) pass pass
Homebrew 23 (RSTUDIO_LIBCLANG_PATH) test-cpp.R:69 fails pass

Also 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:

Build rsession gtests test-cpp.R
#19563 plus this PR 852 pass, including the new test 28 of 28 pass
same, with the helper emitting -I new test fails, 851 pass not run

In 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 -I or -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

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

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.
@kevinushey kevinushey added this to the 2026.12 (Parrot Balsam) milestone Nov 10, 2026
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.
@kevinushey
kevinushey requested a review from gtritchie November 10, 2026 22:41

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to stdDir / builtinDir in ebfa670.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
5473 0 256 4
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1373 0 46 1
✅ desktop-macos 1369 0 51 1
✅ desktop-windows 1368 0 57 1
✅ server-linux 1311 0 54 1
✅ setup 48 0 32 0

View report online


All platforms - commit ebfa670825067df7e3ce192c19e32e8921b86200 - Run #2183

…test

Also rename the 'std' and 'builtin' locals, which shadowed the namespace name.
@kevinushey

Copy link
Copy Markdown
Collaborator Author

Test items fixed in ebfa670. On NEWS: I've left it out. RSTUDIO_LIBCLANG_PATH is an undocumented override (commented as "mostly for debugging" in LibClang.cpp), the default macOS setup isn't affected, and Linux diagnostics are unchanged, so no one on a stock install would see a difference. Happy to add an entry if you'd rather have one.

@kevinushey
kevinushey requested a review from gtritchie November 11, 2026 00:47

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fixes in ebfa670 look good; agreed on leaving the helper-only test and the NEWS entry as they are.

@kevinushey
kevinushey merged commit cfbe4a5 into main Nov 11, 2026
54 checks passed
@kevinushey
kevinushey deleted the bugfix/libclang-system-include-order branch November 11, 2026 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants