Use LLVM's new ItaniumPartialDemangler in LLDB
authorStefan Granitz <stefan.graenitz@gmail.com>
Wed, 25 Jul 2018 15:19:04 +0000 (15:19 +0000)
committerStefan Granitz <stefan.graenitz@gmail.com>
Wed, 25 Jul 2018 15:19:04 +0000 (15:19 +0000)
Summary:
Replace the existing combination of FastDemangle and the fallback to llvm::itaniumDemangle() with LLVM's new ItaniumPartialDemangler. It slightly reduces complexity and slightly improves performance, but doesn't introduce conceptual changes. This patch is preparing for more fundamental improvements on LLDB's demangling approach.

Reviewers: friss, jingham, erik.pilkington, labath, clayborg, mgorny, davide, JDevlieghere

Reviewed By: JDevlieghere

Subscribers: teemperor, JDevlieghere, labath, clayborg, davide, lldb-commits, mgorny, erik.pilkington

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

llvm-svn: 337931

lldb/cmake/modules/LLDBConfig.cmake
lldb/lldb.xcodeproj/project.pbxproj
lldb/source/Core/Mangled.cpp
lldb/unittests/Core/CMakeLists.txt
lldb/unittests/Core/MangledTest.cpp [new file with mode: 0644]

index 32effe7..dae6e36 100644 (file)
@@ -411,15 +411,6 @@ if(LLDB_USING_LIBSTDCXX)
     endif()
 endif()
 
-if(MSVC)
-    set(LLDB_USE_BUILTIN_DEMANGLER ON)
-else()
-    option(LLDB_USE_BUILTIN_DEMANGLER "Use lldb's builtin demangler instead of the system one" ON)
-endif()
-if(LLDB_USE_BUILTIN_DEMANGLER)
-    add_definitions(-DLLDB_USE_BUILTIN_DEMANGLER)
-endif()
-
 if ((CMAKE_SYSTEM_NAME MATCHES "Android") AND LLVM_BUILD_STATIC AND
     ((ANDROID_ABI MATCHES "armeabi") OR (ANDROID_ABI MATCHES "mips")))
   add_definitions(-DANDROID_USE_ACCEPT_WORKAROUND)
index 6ba0f7a..d8a7144 100644 (file)
                9A20573A1F3B8E7E00F6C293 /* MainLoopTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A2057301F3B8E7600F6C293 /* MainLoopTest.cpp */; };
                8C3BD9961EF45DA50016C343 /* MainThreadCheckerRuntime.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C3BD9951EF45D9B0016C343 /* MainThreadCheckerRuntime.cpp */; };
                2689004313353E0400698AC0 /* Mangled.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E8010F1B85900F91463 /* Mangled.cpp */; };
+               4F29D3CF21010FA3003B549A /* MangledTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4F29D3CD21010F84003B549A /* MangledTest.cpp */; };
                4CD44CFC20B37C440003557C /* ManualDWARFIndex.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4CD44CF920B37C440003557C /* ManualDWARFIndex.cpp */; };
                49DCF702170E70120092F75E /* Materializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 49DCF700170E70120092F75E /* Materializer.cpp */; };
                2690B3711381D5C300ECFBAE /* Memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2690B3701381D5C300ECFBAE /* Memory.cpp */; };
                2669415F1A6DC2AB0063BE93 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; name = Makefile; path = "tools/lldb-mi/Makefile"; sourceTree = SOURCE_ROOT; };
                26BC7E8010F1B85900F91463 /* Mangled.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Mangled.cpp; path = source/Core/Mangled.cpp; sourceTree = "<group>"; };
                26BC7D6910F1B77400F91463 /* Mangled.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Mangled.h; path = include/lldb/Core/Mangled.h; sourceTree = "<group>"; };
+               4F29D3CD21010F84003B549A /* MangledTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MangledTest.cpp; sourceTree = "<group>"; };
                4CD44CF920B37C440003557C /* ManualDWARFIndex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ManualDWARFIndex.cpp; sourceTree = "<group>"; };
                4CD44D0020B37C580003557C /* ManualDWARFIndex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ManualDWARFIndex.h; sourceTree = "<group>"; };
                2682100C143A59AE004BCF2D /* MappedHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MappedHash.h; path = include/lldb/Core/MappedHash.h; sourceTree = "<group>"; };
                23CB14E51D66CBEB00EDDDE1 /* Core */ = {
                        isa = PBXGroup;
                        children = (
+                               4F29D3CD21010F84003B549A /* MangledTest.cpp */,
                                9A3D43E31F3237D500EB767C /* ListenerTest.cpp */,
                                9A3D43E21F3237D500EB767C /* StateTest.cpp */,
                                9A3D43E11F3237D500EB767C /* StreamCallbackTest.cpp */,
                                9A3D43D71F3151C400EB767C /* LogTest.cpp in Sources */,
                                9A2057181F3B861400F6C293 /* TestType.cpp in Sources */,
                                9A2057171F3B861400F6C293 /* TestDWARFCallFrameInfo.cpp in Sources */,
+                               4F29D3CF21010FA3003B549A /* MangledTest.cpp in Sources */,
                                9A3D43EC1F3237F900EB767C /* ListenerTest.cpp in Sources */,
                                9A3D43DC1F3151C400EB767C /* TimeoutTest.cpp in Sources */,
                                9A3D43D61F3151C400EB767C /* ConstStringTest.cpp in Sources */,
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                "OTHER_CPLUSPLUSFLAGS[sdk=iphoneos*]" = (
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                OTHER_LDFLAGS = "";
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                "OTHER_CPLUSPLUSFLAGS[sdk=iphoneos*]" = (
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                OTHER_LDFLAGS = "";
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                "OTHER_CPLUSPLUSFLAGS[sdk=iphoneos*]" = (
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                OTHER_LDFLAGS = "";
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                "OTHER_CPLUSPLUSFLAGS[sdk=iphoneos*]" = (
                                        "-fno-rtti",
                                        "-Wglobal-constructors",
                                        "$(OTHER_CFLAGS)",
-                                       "-DLLDB_USE_BUILTIN_DEMANGLER",
                                        "-DLIBXML2_DEFINED",
                                );
                                OTHER_LDFLAGS = "";
index 3f64a3d..545ac51 100644 (file)
 #pragma comment(lib, "dbghelp.lib")
 #endif
 
-#ifdef LLDB_USE_BUILTIN_DEMANGLER
-// Provide a fast-path demangler implemented in FastDemangle.cpp until it can
-// replace the existing C++ demangler with a complete implementation
-#include "lldb/Utility/FastDemangle.h"
-#include "llvm/Demangle/Demangle.h"
-#else
-// FreeBSD9-STABLE requires this to know about size_t in cxxabi.
-#include <cstddef>
-#include <cxxabi.h>
-#endif
-
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/Logging.h"
 #include "lldb/Utility/RegularExpression.h"
 #include "lldb/Utility/Stream.h"
 #include "lldb/Utility/Timer.h"
-#include "lldb/lldb-enumerations.h" // for LanguageType
+#include "lldb/lldb-enumerations.h"
 
 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
 #include "Plugins/Language/ObjC/ObjCLanguage.h"
 
-#include "llvm/ADT/StringRef.h"    // for StringRef
-#include "llvm/Support/Compiler.h" // for LLVM_PRETT...
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Demangle/Demangle.h"
+#include "llvm/Support/Compiler.h"
 
-#include <mutex>   // for mutex, loc...
-#include <string>  // for string
-#include <utility> // for pair
+#include <mutex>
+#include <string>
+#include <utility>
 
 #include <stdlib.h>
 #include <string.h>
@@ -295,18 +285,15 @@ Mangled::GetDemangledName(lldb::LanguageType language) const {
         break;
       }
       case eManglingSchemeItanium: {
-#ifdef LLDB_USE_BUILTIN_DEMANGLER
-        if (log)
-          log->Printf("demangle itanium: %s", mangled_name);
-        // Try to use the fast-path demangler first for the performance win,
-        // falling back to the full demangler only when necessary
-        demangled_name = FastDemangle(mangled_name, m_mangled.GetLength());
-        if (!demangled_name)
-          demangled_name =
-              llvm::itaniumDemangle(mangled_name, NULL, NULL, NULL);
-#else
-        demangled_name = abi::__cxa_demangle(mangled_name, NULL, NULL, NULL);
-#endif
+        llvm::ItaniumPartialDemangler IPD;
+        bool demangle_err = IPD.partialDemangle(mangled_name);
+        if (!demangle_err) {
+          // Default buffer and size (realloc is used in case it's too small).
+          size_t demangled_size = 80;
+          demangled_name = static_cast<char *>(::malloc(demangled_size));
+          demangled_name = IPD.finishDemangle(demangled_name, &demangled_size);
+        }
+
         if (log) {
           if (demangled_name)
             log->Printf("demangled itanium: %s -> \"%s\"", mangled_name,
index f427845..4affb8c 100644 (file)
@@ -3,6 +3,7 @@ add_lldb_unittest(LLDBCoreTests
   DataExtractorTest.cpp
   EventTest.cpp
   ListenerTest.cpp
+  MangledTest.cpp
   ScalarTest.cpp
   StateTest.cpp
   StreamCallbackTest.cpp
diff --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/MangledTest.cpp
new file mode 100644 (file)
index 0000000..7deb901
--- /dev/null
@@ -0,0 +1,38 @@
+//===-- MangledTest.cpp -----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "lldb/Core/Mangled.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+TEST(MangledTest, ResultForValidName) {
+  ConstString MangledName("_ZN1a1b1cIiiiEEvm");
+  bool IsMangled = true;
+
+  Mangled TheMangled(MangledName, IsMangled);
+  const ConstString &TheDemangled =
+      TheMangled.GetDemangledName(eLanguageTypeC_plus_plus);
+
+  ConstString ExpectedResult("void a::b::c<int, int, int>(unsigned long)");
+  EXPECT_STREQ(ExpectedResult.GetCString(), TheDemangled.GetCString());
+}
+
+TEST(MangledTest, EmptyForInvalidName) {
+  ConstString MangledName("_ZN1a1b1cmxktpEEvm");
+  bool IsMangled = true;
+
+  Mangled TheMangled(MangledName, IsMangled);
+  const ConstString &TheDemangled =
+      TheMangled.GetDemangledName(eLanguageTypeC_plus_plus);
+
+  EXPECT_STREQ("", TheDemangled.GetCString());
+}