Skip to content

Commit 6a98fa6

Browse files
Add DemangleSymbol function
1 parent 2c6c6b1 commit 6a98fa6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
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 Demangle(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 Demangle(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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,47 @@
88
#include "clang/Frontend/CompilerInstance.h"
99
#include "clang/Sema/Sema.h"
1010

11-
#include "clang/AST/DeclBase.h"
1211
#include "clang/AST/ASTDumper.h"
12+
#include "clang/AST/Decl.h"
13+
#include "clang/AST/DeclBase.h"
14+
#include "clang/AST/GlobalDecl.h"
1315

1416
#include "gtest/gtest.h"
1517

1618
using namespace TestUtils;
1719
using namespace llvm;
1820
using namespace clang;
1921

22+
TEST(ScopeReflectionTest, DemangleSymbol) {
23+
if (llvm::sys::RunningOnValgrind())
24+
GTEST_SKIP() << "XFAIL due to Valgrind report";
25+
26+
std::string code = R"(
27+
int add(int x, int y) { return x + y; }
28+
int add(double x, double y) { return x + y; }
29+
)";
30+
31+
std::vector<Decl*> Decls;
32+
GetAllTopLevelDecls(code, Decls);
33+
EXPECT_EQ(Decls.size(), 2);
34+
35+
auto Add_int = clang::GlobalDecl(static_cast<clang::NamedDecl*>(Decls[0]));
36+
auto Add_double = clang::GlobalDecl(static_cast<clang::NamedDecl*>(Decls[1]));
37+
38+
std::string mangled_add_int;
39+
std::string mangled_add_double;
40+
compat::maybeMangleDeclName(Add_int, mangled_add_int);
41+
compat::maybeMangleDeclName(Add_double, mangled_add_double);
42+
43+
std::string demangled_add_int = Cpp::Demangle(mangled_add_int);
44+
std::string demangled_add_double = Cpp::Demangle(mangled_add_double);
45+
46+
EXPECT_EQ(demangled_add_int.substr(0, demangled_add_int.find('(')),
47+
Cpp::GetQualifiedCompleteName(Decls[0]));
48+
EXPECT_EQ(demangled_add_double.substr(0, demangled_add_double.find('(')),
49+
Cpp::GetQualifiedCompleteName(Decls[1]));
50+
}
51+
2052
TEST(ScopeReflectionTest, IsAggregate) {
2153
std::vector<Decl *> Decls;
2254
std::string code = R"(

0 commit comments

Comments
 (0)