*/
CINDEX_LINKAGE CXString clang_Cursor_getBriefCommentText(CXCursor C);
+/**
+ * @}
+ */
+
+/** \defgroup CINDEX_MANGLE Name Mangling API Functions
+ *
+ * @{
+ */
+
+/**
+ * \brief Retrieve the CXString representing the mangled name of the cursor.
+ */
+CINDEX_LINKAGE CXString clang_Cursor_getMangling(CXCursor);
+
/**
* @}
*/
--- /dev/null
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu -emit-pch %s -o %t_linux.ast
+// RUN: c-index-test -test-print-mangle %t_linux.ast | FileCheck %s --check-prefix=ITANIUM
+
+// RUN: %clang_cc1 -triple i686-pc-win32 -emit-pch %s -o %t_msft.ast
+// RUN: c-index-test -test-print-mangle %t_msft.ast | FileCheck %s --check-prefix=MICROSOFT
+
+int foo(int, int);
+// ITANIUM: mangled=_Z3fooii
+// MICROSOFT: mangled=?foo@@YAHHH
+
+int foo(float, int);
+// ITANIUM: mangled=_Z3foofi
+// MICROSOFT: mangled=?foo@@YAHMH
+
+struct S {
+ int x, y;
+};
+// ITANIUM: StructDecl{{.*}}mangled=]
+// MICROSOFT: StructDecl{{.*}}mangled=]
+
+int foo(S, S&);
+// ITANIUM: mangled=_Z3foo1SRS
+// MICROSOFT: mangled=?foo@@YAHUS
return CXChildVisit_Recurse;
}
+/******************************************************************************/
+/* Mangling testing. */
+/******************************************************************************/
+
+static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,
+ CXClientData d) {
+ CXString MangledName;
+ PrintCursor(cursor, NULL);
+ MangledName = clang_Cursor_getMangling(cursor);
+ printf(" [mangled=%s]\n", clang_getCString(MangledName));
+ return CXChildVisit_Continue;
+}
+
/******************************************************************************/
/* Bitwidth testing. */
/******************************************************************************/
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-mangle") == 0)
+ return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
if (argc > 2)
return print_usrs(argv + 2, argv + argc);
#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"
return cxloc::translateSourceRange(Ctx, Loc);
}
+CXString clang_Cursor_getMangling(CXCursor C) {
+ 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 || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))
+ return cxstring::createEmpty();
+
+ const NamedDecl *ND = cast<NamedDecl>(D);
+ std::unique_ptr<MangleContext> MC(ND->getASTContext().createMangleContext());
+
+ std::string Buf;
+ llvm::raw_string_ostream OS(Buf);
+ MC->mangleName(ND, OS);
+ OS.flush();
+
+ // The Microsoft mangler may insert a special character in the beginning to
+ // prevent further mangling. We can strip that for display purposes.
+ if (Buf[0] == '\x01') {
+ Buf.erase(0, 1);
+ }
+ return cxstring::createDup(Buf);
+}
+
CXString clang_getCursorDisplayName(CXCursor C) {
if (!clang_isDeclaration(C.kind))
return clang_getCursorSpelling(C);
clang_Cursor_getArgument
clang_Cursor_getBriefCommentText
clang_Cursor_getCommentRange
+clang_Cursor_getMangling
clang_Cursor_getParsedComment
clang_Cursor_getRawCommentText
clang_Cursor_getNumArguments