https://github.com/quic-k updated https://github.com/llvm/llvm-project/pull/183254
>From ff5b667738aed4dfa3450ac6595861c3f6ebed01 Mon Sep 17 00:00:00 2001 From: Kushal Pal <[email protected]> Date: Tue, 24 Feb 2026 12:01:02 +0530 Subject: [PATCH] [Clang] Add clang flag --cstdlib Introduce clang flag --cstdlib based on RFC: https://discourse.llvm.org/t/rfc-add-command-line-option-for-selecting-c-library/87335 This flag accepts a string i.e. the name of the C library that user wants to use. Toolchain drivers can handle this flag as per need or ignore it. Signed-off-by: Kushal Pal <[email protected]> --- clang/include/clang/Driver/ToolChain.h | 6 ++++++ clang/include/clang/Options/Options.td | 4 ++++ clang/lib/Driver/ToolChain.cpp | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h index 665f7f91ecfcd..4a00567766ae4 100644 --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -194,6 +194,7 @@ class ToolChain { mutable std::optional<CXXStdlibType> cxxStdlibType; mutable std::optional<RuntimeLibType> runtimeLibType; mutable std::optional<UnwindLibType> unwindLibType; + mutable std::optional<std::string> cstdlibType; protected: MultilibSet Multilibs; @@ -729,6 +730,11 @@ class ToolChain { // given compilation arguments. virtual UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const; + // GetCStdlibType - Determine the C standard library to use with the given + // compilation arguments. Returns an open-ended string; "system" is the + // default when no --cstdlib= flag is provided. + virtual std::string GetCStdlibType(const llvm::opt::ArgList &Args) const; + // Detect the highest available version of libc++ in include path. virtual std::string detectLibcxxVersion(StringRef IncludePath) const; diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 4ac812e92e2cb..90bcdc629102a 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -6454,6 +6454,9 @@ def std_EQ : Joined<["-", "--"], "std=">, def stdlib_EQ : Joined<["-", "--"], "stdlib=">, Visibility<[ClangOption, CC1Option]>, HelpText<"C++ standard library to use">, Values<"libc++,libstdc++,platform">; +def cstdlib_EQ : Joined<["-", "--"], "cstdlib=">, + Visibility<[ClangOption]>, + HelpText<"C standard library to use">, MetaVarName<"<arg>">; def stdlibxx_isystem : JoinedOrSeparate<["-"], "stdlib++-isystem">, Group<clang_i_Group>, HelpText<"Use directory as the C++ standard library include path">, @@ -6706,6 +6709,7 @@ def _version : Flag<["--"], "version">, def _signed_char : Flag<["--"], "signed-char">, Alias<fsigned_char>; def _std : Separate<["--"], "std">, Alias<std_EQ>; def _stdlib : Separate<["--"], "stdlib">, Alias<stdlib_EQ>; +def _cstdlib : Separate<["--"], "cstdlib">, Alias<cstdlib_EQ>; def _target_help : Flag<["--"], "target-help">; def _trace_includes : Flag<["--"], "trace-includes">, Alias<H>; def _undefine_macro_EQ : Joined<["--"], "undefine-macro=">, Alias<U>; diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp index 4b902b4db0c23..78e42c63906c4 100644 --- a/clang/lib/Driver/ToolChain.cpp +++ b/clang/lib/Driver/ToolChain.cpp @@ -1413,6 +1413,16 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ return *cxxStdlibType; } +std::string ToolChain::GetCStdlibType(const ArgList &Args) const { + if (cstdlibType) + return *cstdlibType; + + const Arg *A = Args.getLastArg(options::OPT_cstdlib_EQ); + cstdlibType = A ? A->getValue() : "system"; + + return *cstdlibType; +} + /// Utility function to add a system framework directory to CC1 arguments. void ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
