Skip to content

[lldb][ClangExpressionParser] Set BuiltinHeadersInSystemModules depending on SDK version #9059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Michael137
Copy link

Abandoned llvm#101778 in favour of this

This patch changes the way we initialize BuiltinHeadersInSystemModules which is one of the flags controlling Clang's behaviour when the Darwin module is split into more fine-grained modules. The ClangExpressionParser currently unconditionally sets -fbuiltin-headers-in-system-modules when evaluating expressions with the target.import-std-module setting. This flag should, however, be set depending on the SDK version (which is what the Clang Darwin toolchain does).

Unfortunately, the compiler instance that we create with ClangExpressionParser never consults the Clang driver, and thus doesn't correctly infer BuiltinHeadersInSystemModules. Note, this isn't an issue with the CompilerInstance that the ClangModulesDeclVendor creates because it uses the createInovcation API, which calls into Darwin::addClangTargetOptions.

This patch mimicks how sdkSupportsBuiltinModules is used in Darwin::addClangTargetOptions.

This ensures that the import-std-module API tests run cleanly regardless of SDK version.

The plan is to eventually make the CompilerInstance construction in ClangExpressionParser go through the driver, so we can avoid duplicating the logic in LLDB. But we aren't there yet.

Implementation

  • We look for the SDKSettings.json in the sysroot directory that we found in DWARF (via DW_AT_LLVM_sysroot)
  • Then parse this file and extract the SDK version number out of it
  • Then mimick sdkSupportsBuiltinModules from Toolchains/Darwin.cpp and set BuiltinHeadersInSystemModules based on it

rdar://116490281

…ding on SDK version

This patch changes the way we initialize `BuiltinHeadersInSystemModules` which is one of the flags controlling Clang's behaviour when the Darwin module is split into more fine-grained modules. The ClangExpressionParser currently unconditionally sets `-fbuiltin-headers-in-system-modules` when evaluating expressions with the `target.import-std-module` setting. This flag should, however, be set depending on the SDK version (which is what the Clang Darwin toolchain does).

Unfortunately, the compiler instance that we create with `ClangExpressionParser` never consults the Clang driver, and thus doesn't correctly infer `BuiltinHeadersInSystemModules`. Note, this isn't an issue with the `CompilerInstance` that the `ClangModulesDeclVendor` creates because it uses the `createInovcation` API, which calls into `Darwin::addClangTargetOptions`.

This patch mimicks how `sdkSupportsBuiltinModules` is used in `Darwin::addClangTargetOptions`.

This ensures that the `import-std-module` API tests run cleanly regardless of SDK version.

The plan is to eventually make the `CompilerInstance` construction in `ClangExpressionParser` go through the driver, so we can avoid duplicating the logic in LLDB. But we aren't there yet.

**Implementation**
* We look for the `SDKSettings.json` in the sysroot directory that we found in DWARF (via `DW_AT_LLVM_sysroot`)
* Then parse this file and extract the SDK version number out of it
* Then mimick `sdkSupportsBuiltinModules` from `Toolchains/Darwin.cpp` and set `BuiltinHeadersInSystemModules` based on it

rdar://116490281
@Michael137
Copy link
Author

@swift-ci test

// NOTE: should be kept in sync with sdkSupportsBuiltinModules in
// Toolchains/Darwin.cpp
static bool
sdkSupportsBuiltinModulesImpl(const llvm::Triple &triple,

Choose a reason for hiding this comment

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

I'm about to change the implementation of this function in the Darwin driver

Copy link
Author

Choose a reason for hiding this comment

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

Implementation in what sense? Changes to the SDK version checks?

Choose a reason for hiding this comment

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

Posted llvm#102239 for it.

Copy link
Author

Choose a reason for hiding this comment

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

Ah yes I accounted for that in this patch already

@ian-twilightcoder
Copy link

ian-twilightcoder commented Aug 6, 2024

@cachemeifyoucan @Bigcheese @cyndyishida is it really that difficult to change this usage to use clang::createInvocation or clang::CompilerInvocation::CreateFromArgs or clang::BuildCompilation?

@Michael137
Copy link
Author

Michael137 commented Aug 6, 2024

@cachemeifyoucan @Bigcheese @cyndyishida is it really that difficult to change this usage to use clang::createInvocation or clang::CompilerInvocation::CreateFromArgs or clang::BuildCompilation?

I have something that works to some extent. But it's definitely a risky enough change to land for 6.0

@cachemeifyoucan
Copy link

is it really that difficult to change this usage to use clang::createInvocation or clang::CompilerInvocation::CreateFromArgs or clang::BuildCompilation?

I have been advocating lldb to change to use them to avoid a lot of the module configuration issues.

@cyndyishida
Copy link

@cachemeifyoucan @Bigcheese @cyndyishida is it really that difficult to change this usage to use clang::createInvocation or clang::CompilerInvocation::CreateFromArgs or clang::BuildCompilation?

I have something that works to some extent. But it's definitely a risky enough change to land for 6.0

Whats the risk involved?

Copy link

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

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

LGTM as a surgical fix for the swift/release/6.0 branch.

@@ -576,7 +644,8 @@ ClangExpressionParser::ClangExpressionParser(
lang_opts.GNUMode = true;
lang_opts.GNUKeywords = true;
lang_opts.CPlusPlus11 = true;
lang_opts.BuiltinHeadersInSystemModules = true;
lang_opts.BuiltinHeadersInSystemModules = !sdkSupportsBuiltinModules(

Choose a reason for hiding this comment

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

This ! is the intended direction, right?

Choose a reason for hiding this comment

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

Yes. The builtin headers have to go in the system modules if the sdk doesn't support the builtin modules.

@adrian-prantl
Copy link

@cachemeifyoucan @Bigcheese @cyndyishida is it really that difficult to change this usage to use clang::createInvocation or clang::CompilerInvocation::CreateFromArgs or clang::BuildCompilation?

I have something that works to some extent. But it's definitely a risky enough change to land for 6.0

Whats the risk involved?

I wouldn't want to change the way we setup up the CompilerInvocation this late in the release branch's life.

@adrian-prantl adrian-prantl merged commit 3e28ce1 into swift/release/6.0 Aug 7, 2024
3 checks passed
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.

5 participants