Skip to content

Commit fb60be4

Browse files
Add Demangle function
1 parent 39e13cb commit fb60be4

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-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+
#ifdef _WIN32
162+
std::string demangle = microsoftDemangle(mangled_name, nullptr, nullptr);
163+
#else
164+
std::string demangle = itaniumDemangle(mangled_name);
165+
#endif
166+
#else
167+
#ifdef _WIN32
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: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,51 @@
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"
15+
16+
#include "llvm/Support/Valgrind.h"
1317

1418
#include "gtest/gtest.h"
1519

20+
#include <string>
21+
1622
using namespace TestUtils;
1723
using namespace llvm;
1824
using namespace clang;
1925

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

0 commit comments

Comments
 (0)