https://github.com/Michael137 created https://github.com/llvm/llvm-project/pull/182297
Enables `-gsimple-template-names=simple` when targeting recent Apple platforms (26 or later, except `DriverKit` which is at 25). Those are platforms where the associated LLDB is capable of debugging `simple-template-names` debug-info. The two main affects on debug-info are: 1. forward declarations for structures now have `DW_TAG_type_template_parameter`s (since this is required to reconstruct the template names if just given a forward declaration 2. `DW_AT_name` of templates will not include template parameters anymore (except for a few cases where the name is not reconstitutible from the template parameter DIE names) While the `.debug_str` section is reduced in size (due to shorter `DW_AT_name`s), this is somewhat offset by having to include template parameter DIEs on forward declarations. >From 36008ef28bfbaaf52c1d8e3fc561c0a702223329 Mon Sep 17 00:00:00 2001 From: Michael Buch <[email protected]> Date: Tue, 9 Dec 2025 15:21:01 +0000 Subject: [PATCH] [clang][Driver][Darwin] Turn on -gsimple-template-names for Darwin by default Enables `-gsimple-template-names=simple` when targeting recent Apple platforms (26 or later, except `DriverKit` which is at 25). Those are platforms where the associated LLDB is capable of debugging `simple-template-names` debug-info. The two main affects on debug-info are: 1. forward declarations for structures now have `DW_TAG_type_template_parameter`s (since this is required to reconstruct the template names if just given a forward declaration 2. `DW_AT_name` of templates will not include template parameters anymore (except for a few cases where the name is not reconstitutible from the template parameter DIE names) While the `.debug_str` section is reduced in size (due to shorter `DW_AT_name`s), this is somewhat offset by having to include template parameter DIEs on forward declarations. --- clang/include/clang/Driver/ToolChain.h | 4 ++++ clang/lib/Driver/ToolChains/Clang.cpp | 30 ++++++++++++++++-------- clang/lib/Driver/ToolChains/Darwin.cpp | 15 ++++++++++++ clang/lib/Driver/ToolChains/Darwin.h | 2 ++ clang/test/DebugInfo/CXX/fn-template.cpp | 2 +- clang/test/Driver/debug-options.c | 21 ++++++++++++++--- 6 files changed, 60 insertions(+), 14 deletions(-) diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h index 16573a6589813..3f79bcad344cc 100644 --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -615,6 +615,10 @@ class ToolChain { // i.e. a value of 'true' does not imply that debugging is wanted. virtual bool GetDefaultStandaloneDebug() const { return false; } + /// Returns true if this toolchain adds '-gsimple-template-names=simple' + /// by default when generating debug-info. + virtual bool getDefaultDebugSimpleTemplateNames() const { return false; } + // Return the default debugger "tuning." virtual llvm::DebuggerKind getDefaultDebuggerTuning() const { return llvm::DebuggerKind::GDB; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 8aa2c595e2dea..c2f09ccdc314b 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -4349,6 +4349,22 @@ static void renderDwarfFormat(const Driver &D, const llvm::Triple &T, DwarfFormatArg->render(Args, CmdArgs); } +static bool getDebugSimpleTemplateNames(const ToolChain &TC, const Driver &D, + const ArgList &Args) { + bool NeedsSimpleTemplateNames = + Args.hasFlag(options::OPT_gsimple_template_names, + options::OPT_gno_simple_template_names, + TC.getDefaultDebugSimpleTemplateNames()); + if (!NeedsSimpleTemplateNames) + return false; + + if (const Arg *A = Args.getLastArg(options::OPT_gsimple_template_names)) + if (!checkDebugInfoOption(A, Args, D, TC)) + return false; + + return true; +} + static void renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T, const ArgList &Args, types::ID InputType, @@ -4633,17 +4649,11 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T, ? "-gpubnames" : "-ggnu-pubnames"); } - const auto *SimpleTemplateNamesArg = - Args.getLastArg(options::OPT_gsimple_template_names, - options::OPT_gno_simple_template_names); + bool ForwardTemplateParams = DebuggerTuning == llvm::DebuggerKind::SCE; - if (SimpleTemplateNamesArg && - checkDebugInfoOption(SimpleTemplateNamesArg, Args, D, TC)) { - const auto &Opt = SimpleTemplateNamesArg->getOption(); - if (Opt.matches(options::OPT_gsimple_template_names)) { - ForwardTemplateParams = true; - CmdArgs.push_back("-gsimple-template-names=simple"); - } + if (getDebugSimpleTemplateNames(TC, D, Args)) { + ForwardTemplateParams = true; + CmdArgs.push_back("-gsimple-template-names=simple"); } // Emit DW_TAG_template_alias for template aliases? True by default for SCE. diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp index 1c95a79a52a9c..30696c122f996 100644 --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -1369,6 +1369,21 @@ unsigned DarwinClang::GetDefaultDwarfVersion() const { return 5; } +bool DarwinClang::getDefaultDebugSimpleTemplateNames() const override { + // Default to an OS version on which LLDB supports debugging -gsimple-template-names + // programs. + if ((isTargetMacOSBased() && isMacosxVersionLT(26)) || + (isTargetIOSBased() && isIPhoneOSVersionLT(26)) || + (isTargetWatchOSBased() && TargetVersion < llvm::VersionTuple(26)) || + (isTargetXROS() && TargetVersion < llvm::VersionTuple(26)) || + (isTargetDriverKit() && TargetVersion < llvm::VersionTuple(25)) || + (isTargetMacOSBased() && + TargetVersion.empty())) // apple-darwin, no version. + return false; + + return true; +} + void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs, StringRef Component, RuntimeLinkOptions Opts, bool IsShared) const { diff --git a/clang/lib/Driver/ToolChains/Darwin.h b/clang/lib/Driver/ToolChains/Darwin.h index 14a80c973485a..75f1dff46bfa9 100644 --- a/clang/lib/Driver/ToolChains/Darwin.h +++ b/clang/lib/Driver/ToolChains/Darwin.h @@ -693,6 +693,8 @@ class LLVM_LIBRARY_VISIBILITY DarwinClang : public Darwin { return llvm::DebuggerKind::LLDB; } + bool getDefaultDebugSimpleTemplateNames() const override; + /// } private: diff --git a/clang/test/DebugInfo/CXX/fn-template.cpp b/clang/test/DebugInfo/CXX/fn-template.cpp index 2aed4bef71c8e..03af3d588e4b5 100644 --- a/clang/test/DebugInfo/CXX/fn-template.cpp +++ b/clang/test/DebugInfo/CXX/fn-template.cpp @@ -1,4 +1,4 @@ -// RUN: %clang -emit-llvm -g -S %s -o - | FileCheck %s +// RUN: %clang -emit-llvm -g -gno-simple-template-names -S %s -o - | FileCheck %s template<typename T> struct XF { diff --git a/clang/test/Driver/debug-options.c b/clang/test/Driver/debug-options.c index 27e2728f15948..92179ff9421ad 100644 --- a/clang/test/Driver/debug-options.c +++ b/clang/test/Driver/debug-options.c @@ -486,12 +486,27 @@ // DIRECTORY-NOT: "-fno-dwarf-directory-asm" // NODIRECTORY: "-fno-dwarf-directory-asm" -// RUN: %clang -### -target x86_64 -c -g -gsimple-template-names %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-linux -c -g -gsimple-template-names %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-macosx26.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-ios26.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-tvos26.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-xros26.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-watchos26.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-driverkit25.0 -c -g %s 2>&1 | FileCheck --check-prefixes=SIMPLE_TMPL_NAMES,FWD_TMPL_PARAMS %s +// RUN: %clang -### -target x86_64-apple-macosx10.11 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s +// RUN: %clang -### -target x86_64-apple-ios10.11 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s +// RUN: %clang -### -target x86_64-apple-tvos10.11 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s +// RUN: %clang -### -target x86_64-apple-xros1 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s +// RUN: %clang -### -target x86_64-apple-watchos10.11 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s +// RUN: %clang -### -target x86_64-apple-driverkit19.11 -c -g %s 2>&1 | FileCheck --implicit-check-not=-gsimple-template-names --implicit-check-not=-debug-forward-template-params %s + // SIMPLE_TMPL_NAMES: -gsimple-template-names=simple // FWD_TMPL_PARAMS-DAG: -debug-forward-template-params -// RUN: not %clang -### -target x86_64 -c -g -gsimple-template-names=mangled %s 2>&1 | FileCheck --check-prefix=MANGLED_TEMP_NAMES %s + +// RUN: not %clang -### -target x86_64-linux -c -g -gsimple-template-names=mangled %s 2>&1 | FileCheck --check-prefix=MANGLED_TEMP_NAMES %s // MANGLED_TEMP_NAMES: error: unknown argument '-gsimple-template-names=mangled'; did you mean '-Xclang -gsimple-template-names=mangled' -// RUN: %clang -### -target x86_64 -c -g %s 2>&1 | FileCheck --check-prefix=FULL_TEMP_NAMES --implicit-check-not=debug-forward-template-params %s + +// RUN: %clang -### -target x86_64-linux -c -g %s 2>&1 | FileCheck --check-prefix=FULL_TEMP_NAMES --implicit-check-not=debug-forward-template-params %s // FULL_TEMP_NAMES-NOT: -gsimple-template-names //// Test -g[no-]template-alias (enabled by default with SCE debugger tuning and DWARF version >= 4). _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
