Skip to content

Commit 690d44c

Browse files
Add DemangleSymbol function
1 parent 2c6c6b1 commit 690d44c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ namespace Cpp {
185185
///\returns the version string information of the library.
186186
CPPINTEROP_API std::string GetVersion();
187187

188+
///\returns the demangled representation of the given mangled_name
189+
CPPINTEROP_API std::string DemangleSymbol(const std::string& mangled_name);
190+
188191
/// Enables or disables the debugging printouts on stderr.
189192
/// Debugging output can be enabled also by the environment variable
190193
/// CPPINTEROP_EXTRA_INTERPRETER_ARGS. For example,

lib/Interpreter/CppInterOp.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "llvm/ADT/SmallVector.h"
3333
#include "llvm/ADT/StringRef.h"
34+
#include "llvm/Demangle/Demangle.h"
3435
#include "llvm/Support/Casting.h"
3536
#include "llvm/Support/Debug.h"
3637
#include "llvm/Support/raw_os_ostream.h"
@@ -155,6 +156,25 @@ namespace Cpp {
155156
return fullVersion + "[" + clang::getClangFullVersion() + "])\n";
156157
}
157158

159+
std::string DemangleSymbol(const std::string& mangled_name) {
160+
#if CLANG_VERSION_MAJOR > 16
161+
#if defined(MSVC)
162+
std::string demangle = microsoftDemangle(mangled_name, nullptr, nullptr);
163+
#else
164+
std::string demangle = itaniumDemangle(mangled_name);
165+
#endif
166+
#else
167+
#ifdef MSVC
168+
std::string demangle = microsoftDemangle(mangled_name.c_str(), nullptr,
169+
nullptr, nullptr, nullptr);
170+
#else
171+
std::string demangle =
172+
itaniumDemangle(mangled_name.c_str(), nullptr, nullptr, nullptr);
173+
#endif
174+
#endif
175+
return demangle;
176+
}
177+
158178
void EnableDebugOutput(bool value/* =true*/) {
159179
llvm::DebugFlag = value;
160180
}

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ using namespace TestUtils;
1717
using namespace llvm;
1818
using namespace clang;
1919

20+
TEST(ScopeReflectionTest, DemangleSymbol) {
21+
if (llvm::sys::RunningOnValgrind())
22+
GTEST_SKIP() << "XFAIL due to Valgrind report";
23+
24+
std::string code = R"(
25+
#include <string>
26+
#include <typeinfo>
27+
28+
class C { public: virtual ~C() {} };
29+
namespace scope {
30+
class C { public: virtual ~C() {} };
31+
}
32+
33+
const char* c = typeid(C).name();
34+
const char* scope_c = typeid(scope::C).name();
35+
)";
36+
37+
EXPECT_EQ(Cpp::Declare(code.c_str()), 0);
38+
39+
Cpp::TCppScope_t C = Cpp::GetNamed("C");
40+
Cpp::TCppScope_t scope_C = Cpp::GetNamed("C", Cpp::GetNamed("scope"));
41+
Cpp::TCppScope_t c = Cpp::GetNamed("c");
42+
Cpp::TCppScope_t scope_c = Cpp::GetNamed("scope_c");
43+
44+
char* mangled_C = *(char**)Cpp::GetVariableOffset(c);
45+
char* mangled_scope_C = *(char**)Cpp::GetVariableOffset(scope_c);
46+
47+
EXPECT_EQ(Cpp::DemangleSymbol(mangled_C), Cpp::GetQualifiedCompleteName(C));
48+
EXPECT_EQ(Cpp::DemangleSymbol(mangled_scope_C),
49+
Cpp::GetQualifiedCompleteName(scope_C));
50+
}
51+
2052
TEST(ScopeReflectionTest, IsAggregate) {
2153
std::vector<Decl *> Decls;
2254
std::string code = R"(

0 commit comments

Comments
 (0)