[Lldb-commits] [lldb] b5e49e9 - [lldb] Ignore certain Clang type sugar when creating the type name

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Tue Sep 22 04:37:53 PDT 2020


Author: Raphael Isemann
Date: 2020-09-22T13:37:20+02:00
New Revision: b5e49e91cb90eda1f926139c8567e27f1b664cc1

URL: https://github.com/llvm/llvm-project/commit/b5e49e91cb90eda1f926139c8567e27f1b664cc1
DIFF: https://github.com/llvm/llvm-project/commit/b5e49e91cb90eda1f926139c8567e27f1b664cc1.diff

LOG: [lldb] Ignore certain Clang type sugar when creating the type name

Clang has some type sugar that only serves as a way to preserve the way a user
has typed a certain type in the source code. These types are currently not
unwrapped when we query the type name for a Clang type, which means that this
type sugar actually influences what formatters are picked for a certain type.
Currently if a user decides to reference a type by doing `::GlobalDecl Var = 3;`,
the type formatter for `GlobalDecl` will not be used (as the type sugar
around the type gives it the name `::GlobalDecl`. The same goes for other ways
to spell out a type such as `auto` etc.

With this patch most of this type sugar gets stripped when the full type name is
calculated. Typedefs are not getting desugared as that seems counterproductive.
I also don't desugar atomic types as that's technically not type sugar.

Reviewed By: jarin

Differential Revision: https://reviews.llvm.org/D87481

Added: 
    lldb/test/API/lang/cpp/elaborated-types/Makefile
    lldb/test/API/lang/cpp/elaborated-types/TestElaboratedTypes.py
    lldb/test/API/lang/cpp/elaborated-types/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 d80d07574073..ee6fc895d585 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -3639,6 +3639,16 @@ ConstString TypeSystemClang::GetTypeName(lldb::opaque_compiler_type_t type) {
 
   clang::QualType qual_type(GetQualType(type));
 
+  // Remove certain type sugar from the name. Sugar such as elaborated types
+  // or template types which only serve to improve diagnostics shouldn't
+  // act as their own types from the user's perspective (e.g., formatter
+  // shouldn't format a variable 
diff erently depending on how the ser has
+  // specified the type. '::Type' and 'Type' should behave the same).
+  // Typedefs and atomic derived types are not removed as they are actually
+  // useful for identifiying specific types.
+  qual_type = RemoveWrappingTypes(qual_type,
+                                  {clang::Type::Typedef, clang::Type::Atomic});
+
   // For a typedef just return the qualified name.
   if (const auto *typedef_type = qual_type->getAs<clang::TypedefType>()) {
     const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();

diff  --git a/lldb/test/API/lang/cpp/elaborated-types/Makefile b/lldb/test/API/lang/cpp/elaborated-types/Makefile
new file mode 100644
index 000000000000..99998b20bcb0
--- /dev/null
+++ b/lldb/test/API/lang/cpp/elaborated-types/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules

diff  --git a/lldb/test/API/lang/cpp/elaborated-types/TestElaboratedTypes.py b/lldb/test/API/lang/cpp/elaborated-types/TestElaboratedTypes.py
new file mode 100644
index 000000000000..395ba30b5454
--- /dev/null
+++ b/lldb/test/API/lang/cpp/elaborated-types/TestElaboratedTypes.py
@@ -0,0 +1,40 @@
+"""
+Test elaborated types (e.g. Clang's ElaboratedType or TemplateType sugar).
+"""
+
+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()
+        self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+
+        # Add a type formatter for 'Struct'.
+        self.expect("type summary add Struct --summary-string '(summary x=${var.x})'")
+        # Check that creating an expr with an elaborated type ('::Struct')
+        # triggers our formatter for 'Struct' while keeping the elaborated type
+        # as the display type.
+        result = self.expect_expr("::Struct s; s.x = 4; s",
+                                  result_type="::Struct",
+                                  result_summary="(summary x=4)")
+        # Test that a plain elaborated type is only in the display type name but
+        # not in the full type name.
+        self.assertEqual(result.GetTypeName(), "Struct")
+
+        # Test the same for template types (that also only act as sugar to better
+        # show how the template was specified by the user).
+
+        # Declare a template that can actually be instantiated.
+        # FIXME: The error message here is incorrect.
+        self.expect("expr --top-level -- template<typename T> struct $V {};",
+                    error=True, substrs=["Couldn't find $__lldb_expr() in the module"])
+        result = self.expect_expr("$V<::Struct> s; s",
+                                  result_type="$V< ::Struct>")
+        self.assertEqual(result.GetTypeName(), "$V<Struct>")

diff  --git a/lldb/test/API/lang/cpp/elaborated-types/main.cpp b/lldb/test/API/lang/cpp/elaborated-types/main.cpp
new file mode 100644
index 000000000000..2a77663897a5
--- /dev/null
+++ b/lldb/test/API/lang/cpp/elaborated-types/main.cpp
@@ -0,0 +1,9 @@
+struct Struct {
+  int x;
+};
+
+int main() {
+  Struct use;
+  use.x = 3;
+  return use.x; // break here
+}


        


More information about the lldb-commits mailing list