Hi rsmith, rengolin,
Exposes a C API to name mangling for a given cursor.
This is loosely based on https://gist.github.com/tritao/2766291, and was
previously discussed on cfe-dev:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-June/037577.html
Adding testing capability via c-index-test.
http://reviews.llvm.org/D4663
Files:
include/clang-c/Index.h
test/Index/print-mangled-name.cpp
tools/c-index-test/c-index-test.c
tools/libclang/CIndex.cpp
tools/libclang/libclang.exports
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -3639,6 +3639,25 @@
* @}
*/
+/** \defgroup CINDEX_MANGLE Name Mangling API Functions
+ *
+ * @{
+ */
+
+/**
+ * \brief Represents the different available ABIs for name mangling.
+ */
+typedef enum { CXMangleABI_Itanium = 0, CXMangleABI_Microsoft = 1 } CXMangleABI;
+
+/**
+ * \brief Retrieve the CXString representing the mangled name of the cursor.
+ */
+CINDEX_LINKAGE CXString clang_Cursor_getMangling(CXCursor, CXMangleABI);
+
+/**
+ * @}
+ */
+
/**
* \defgroup CINDEX_MODULE Module introspection
*
Index: test/Index/print-mangled-name.cpp
===================================================================
--- /dev/null
+++ test/Index/print-mangled-name.cpp
@@ -0,0 +1,14 @@
+// RUN: c-index-test -test-print-mangled-name %s | FileCheck %s
+
+int foo(int, int);
+// CHECK: mangled=_Z3fooii
+
+int foo(float, int);
+// CHECK: mangled=_Z3foofi
+
+struct S {
+ int x, y;
+};
+
+int foo(S, S&);
+// CHECK: mangled=_Z3foo1SRS
Index: tools/c-index-test/c-index-test.c
===================================================================
--- tools/c-index-test/c-index-test.c
+++ tools/c-index-test/c-index-test.c
@@ -1363,6 +1363,19 @@
}
/******************************************************************************/
+/* Mangling testing. */
+/******************************************************************************/
+
+static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,
+ CXClientData d) {
+ CXString MangledName;
+ PrintCursor(cursor, NULL);
+ MangledName = clang_Cursor_getMangling(cursor, CXMangleABI_Itanium);
+ printf(" [mangled=%s]\n", clang_getCString(MangledName));
+ return CXChildVisit_Continue;
+}
+
+/******************************************************************************/
/* Bitwidth testing. */
/******************************************************************************/
@@ -4081,6 +4094,9 @@
else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
return perform_test_load_source(argc - 2, argv + 2, "all",
PrintBitWidth, 0);
+ else if (argc > 2 && strcmp(argv[1], "-test-print-mangled-name") == 0)
+ return perform_test_load_source(argc - 2, argv + 2, "all",
+ PrintMangledName, 0);
else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
if (argc > 2)
return print_usrs(argv + 2, argv + argc);
Index: tools/libclang/CIndex.cpp
===================================================================
--- tools/libclang/CIndex.cpp
+++ tools/libclang/CIndex.cpp
@@ -22,6 +22,7 @@
#include "CXType.h"
#include "CursorVisitor.h"
#include "clang/AST/Attr.h"
+#include "clang/AST/Mangle.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticCategories.h"
@@ -3667,6 +3668,37 @@
return cxloc::translateSourceRange(Ctx, Loc);
}
+CXString clang_Cursor_getMangling(CXCursor C, CXMangleABI ABI) {
+ if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))
+ return cxstring::createEmpty();
+
+ const Decl *D = getCursorDecl(C);
+ // Mangling only works for functions and variables.
+ if (!D || !(llvm::isa<FunctionDecl>(D) || llvm::isa<VarDecl>(D)))
+ return cxstring::createEmpty();
+
+ const NamedDecl *ND = llvm::cast<NamedDecl>(D);
+ ASTContext &Context = ND->getASTContext();
+ std::unique_ptr<MangleContext> MC;
+
+ switch (ABI) {
+ default:
+ llvm_unreachable("Unknown mangling ABI");
+ break;
+ case CXMangleABI_Itanium:
+ MC.reset(ItaniumMangleContext::create(Context, Context.getDiagnostics()));
+ break;
+ case CXMangleABI_Microsoft:
+ MC.reset(MicrosoftMangleContext::create(Context, Context.getDiagnostics()));
+ break;
+ }
+
+ std::string Buf;
+ llvm::raw_string_ostream OS(Buf);
+ MC->mangleName(ND, OS);
+ return cxstring::createDup(OS.str());
+}
+
CXString clang_getCursorDisplayName(CXCursor C) {
if (!clang_isDeclaration(C.kind))
return clang_getCursorSpelling(C);
Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -9,6 +9,7 @@
clang_Cursor_getArgument
clang_Cursor_getBriefCommentText
clang_Cursor_getCommentRange
+clang_Cursor_getMangling
clang_Cursor_getParsedComment
clang_Cursor_getRawCommentText
clang_Cursor_getNumArguments
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits