@@ -319,6 +319,49 @@ class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
319319 StringRef m_filename;
320320};
321321
322+ // / Returns true if the SDK for the specified triple supports
323+ // / builtin modules in system headers. This is used to decide
324+ // / whether to pass -fbuiltin-headers-in-system-modules to
325+ // / the compiler instance when compiling the `std` module.
326+ static llvm::Expected<bool >
327+ sdkSupportsBuiltinModules (lldb_private::Target &target) {
328+ auto arch_spec = target.GetArchitecture ();
329+ auto const &triple = arch_spec.GetTriple ();
330+ auto module_sp = target.GetExecutableModule ();
331+ if (!module_sp)
332+ return llvm::createStringError (" Executable module not found." );
333+
334+ // Get SDK path that the target was compiled against.
335+ auto platform_sp = target.GetPlatform ();
336+ if (!platform_sp)
337+ return llvm::createStringError (" No Platform plugin found on target." );
338+
339+ auto sdk_or_err = platform_sp->GetSDKPathFromDebugInfo (*module_sp);
340+ if (!sdk_or_err)
341+ return sdk_or_err.takeError ();
342+
343+ // Use the SDK path from debug-info to find a local matching SDK directory.
344+ auto sdk_path_or_err =
345+ HostInfo::GetSDKRoot (HostInfo::SDKOptions{std::move (sdk_or_err->first )});
346+ if (!sdk_path_or_err)
347+ return sdk_path_or_err.takeError ();
348+
349+ auto VFS = FileSystem::Instance ().GetVirtualFileSystem ();
350+ if (!VFS)
351+ return llvm::createStringError (" No virtual filesystem available." );
352+
353+ // Extract SDK version from the /path/to/some.sdk/SDKSettings.json
354+ auto parsed_or_err = clang::parseDarwinSDKInfo (*VFS, *sdk_path_or_err);
355+ if (!parsed_or_err)
356+ return parsed_or_err.takeError ();
357+
358+ auto maybe_sdk = *parsed_or_err;
359+ if (!maybe_sdk)
360+ return llvm::createStringError (" Couldn't find Darwin SDK info." );
361+
362+ return XcodeSDK::SDKSupportsBuiltinModules (triple, maybe_sdk->getVersion ());
363+ }
364+
322365static void SetupModuleHeaderPaths (CompilerInstance *compiler,
323366 std::vector<std::string> include_directories,
324367 lldb::TargetSP target_sp) {
@@ -662,6 +705,7 @@ static void SetupLangOpts(CompilerInstance &compiler,
662705
663706static void SetupImportStdModuleLangOpts (CompilerInstance &compiler,
664707 lldb_private::Target &target) {
708+ Log *log = GetLog (LLDBLog::Expressions);
665709 LangOptions &lang_opts = compiler.getLangOpts ();
666710 lang_opts.Modules = true ;
667711 // We want to implicitly build modules.
@@ -679,7 +723,12 @@ static void SetupImportStdModuleLangOpts(CompilerInstance &compiler,
679723 lang_opts.GNUKeywords = true ;
680724 lang_opts.CPlusPlus11 = true ;
681725
682- lang_opts.BuiltinHeadersInSystemModules = false ;
726+ if (auto supported_or_err = sdkSupportsBuiltinModules (target))
727+ lang_opts.BuiltinHeadersInSystemModules = !*supported_or_err;
728+ else
729+ LLDB_LOG_ERROR (log, supported_or_err.takeError (),
730+ " Failed to determine BuiltinHeadersInSystemModules when "
731+ " setting up import-std-module: {0}" );
683732
684733 // The Darwin libc expects this macro to be set.
685734 lang_opts.GNUCVersion = 40201 ;
0 commit comments