Author: Raphael Isemann Date: 2020-11-30T16:40:50+01:00 New Revision: e0e7bbeb545516c50a0354efc34d329453558c9c
URL: https://github.com/llvm/llvm-project/commit/e0e7bbeb545516c50a0354efc34d329453558c9c DIFF: https://github.com/llvm/llvm-project/commit/e0e7bbeb545516c50a0354efc34d329453558c9c.diff LOG: [lldb] Always include template arguments that have their default value in the internal type name Our type formatters/summaries match on the internal type name we generate in LLDB for Clang types. These names were generated using Clang's default printing policy. However Clang's default printing policy got tweaked over the last month to make the generated type names more readable (by for example excluding inline/anonymous namespaces and removing template arguments that have their default value). This broke the formatter system where LLDB's matching logic now no longer can format certain types as the new type names generated by Clang's default printing policy no longer match the type names that LLDB/the user specified. I already introduced LLDB's own type printing policy and fixed the inline/anonymous namespaces in da121fff1184267a405f81a87f7314df2d474e1c (just to get the test suite passing again). This patch is restoring the old type printing behaviour where always include the template arguments in the internal type name (even if they match the default args). This should get template type formatters/summaries working again in the rare situation where we do know template default arguments within LLDB. This can only happen when either having a template that was parsed in the expression parser or when we get type information from a C++ module. The Clang change that removed defaulted template arguments from Clang's printing policy was e7f3e2103cdb567dda4fd52f81bf4bc07179f5a8 Reviewed By: labath Differential Revision: https://reviews.llvm.org/D92311 Added: lldb/test/API/lang/cpp/default-template-args/Makefile lldb/test/API/lang/cpp/default-template-args/TestDefaultTemplateArgs.py lldb/test/API/lang/cpp/default-template-args/main.cpp Modified: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp Removed: ################################################################################ diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 77470486dd45..bab50a3b068c 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -1998,6 +1998,18 @@ PrintingPolicy TypeSystemClang::GetTypePrintingPolicy() { // and libstdc++ are diff erentiated by their inline namespaces). printing_policy.SuppressInlineNamespace = false; printing_policy.SuppressUnwrittenScope = false; + // Default arguments are also always important for type formatters. Otherwise + // we would need to always specify two type names for the setups where we do + // know the default arguments and where we don't know default arguments. + // + // For example, without this we would need to have formatters for both: + // std::basic_string<char> + // and + // std::basic_string<char, std::char_traits<char>, std::allocator<char> > + // to support setups where LLDB was able to reconstruct default arguments + // (and we then would have suppressed them from the type name) and also setups + // where LLDB wasn't able to reconstruct the default arguments. + printing_policy.SuppressDefaultTemplateArgs = false; return printing_policy; } diff --git a/lldb/test/API/lang/cpp/default-template-args/Makefile b/lldb/test/API/lang/cpp/default-template-args/Makefile new file mode 100644 index 000000000000..99998b20bcb0 --- /dev/null +++ b/lldb/test/API/lang/cpp/default-template-args/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/test/API/lang/cpp/default-template-args/TestDefaultTemplateArgs.py b/lldb/test/API/lang/cpp/default-template-args/TestDefaultTemplateArgs.py new file mode 100644 index 000000000000..970c3522a175 --- /dev/null +++ b/lldb/test/API/lang/cpp/default-template-args/TestDefaultTemplateArgs.py @@ -0,0 +1,41 @@ +""" +Test default template arguments. +""" + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + +class TestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + @no_debug_info_test + def test(self): + self.build() + lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp")) + + # Declare a template with a template argument that has a default argument. + self.expect("expr --top-level -- template<typename T = int> struct $X { int v; };") + + # The type we display to the user should omit the argument with the default + # value. + result = self.expect_expr("$X<> x; x", result_type="$X<>") + # The internal name should also always show all arguments (even if they + # have their default value). + self.assertEqual(result.GetTypeName(), "$X<int>") + + # Test the template but this time specify a non-default value for the + # template argument. + # Both internal type name and the one we display to the user should + # show the non-default value in the type name. + result = self.expect_expr("$X<long> x; x", result_type="$X<long>") + self.assertEqual(result.GetTypeName(), "$X<long>") + + # Test that the formatters are using the internal type names that + # always include all template arguments. + self.expect("type summary add '$X<int>' --summary-string 'summary1'") + self.expect_expr("$X<> x; x", result_summary="summary1") + self.expect("type summary add '$X<long>' --summary-string 'summary2'") + self.expect_expr("$X<long> x; x", result_summary="summary2") diff --git a/lldb/test/API/lang/cpp/default-template-args/main.cpp b/lldb/test/API/lang/cpp/default-template-args/main.cpp new file mode 100644 index 000000000000..ba45ee316cd4 --- /dev/null +++ b/lldb/test/API/lang/cpp/default-template-args/main.cpp @@ -0,0 +1,3 @@ +int main() { + return 0; // break here +} _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits