Skip to content

Instantly share code, notes, and snippets.

@tritao
Created May 22, 2012 03:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tritao/2766291 to your computer and use it in GitHub Desktop.
Save tritao/2766291 to your computer and use it in GitHub Desktop.
libclang Name Mangling API
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h (revision 156043)
+++ include/clang-c/Index.h (working copy)
@@ -4737,10 +4737,32 @@
* @}
*/
+/** \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);
+
+/**
* @}
*/
+/**
+ * @}
+ */
+
#ifdef __cplusplus
}
#endif
Index: tools/libclang/CXCursor.cpp
===================================================================
--- tools/libclang/CXCursor.cpp (revision 156043)
+++ tools/libclang/CXCursor.cpp (working copy)
@@ -24,6 +24,7 @@
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
+#include "clang/AST/Mangle.h"
#include "clang-c/Index.h"
#include "llvm/Support/ErrorHandling.h"
@@ -1253,5 +1254,39 @@
pool.AvailableCursors.push_back(Vec);
}
+
+CXString clang_Cursor_getMangling(CXCursor C, CXMangleABI ABI) {
+ if (clang_isInvalid(C.kind) ||
+ !clang_isDeclaration(C.kind))
+ return cxstring::createCXString("");
+
+ Decl* D = getCursorDecl(C);
+
+ if(!D || !isa<NamedDecl>(D))
+ return cxstring::createCXString("");
+
+ NamedDecl* ND = static_cast<NamedDecl*>(D);
+
+ ASTContext& AC = ND->getASTContext();
+
+ llvm::OwningPtr<MangleContext> MC;
+
+ switch(ABI) {
+ default:
+ llvm_unreachable("Unknown mangling ABI");
+ break;
+ case CXMangleABI_Itanium:
+ MC.reset(createItaniumMangleContext(AC, AC.getDiagnostics()));
+ break;
+ case CXMangleABI_Microsoft:
+ MC.reset(createMicrosoftMangleContext(AC, AC.getDiagnostics()));
+ break;
+ }
+
+ std::string Mangled;
+ MC->mangleName(ND, llvm::raw_string_ostream(Mangled));
+
+ return cxstring::createCXString(Mangled);
+}
} // end: extern "C"
@ihnorton
Copy link

@tritao did you ever propose this as a formal patch? I would be interested in seeing this in libclang - have done a similar thing in my project (not as neatly, and only for Itanium).

@eliben
Copy link

eliben commented Jul 31, 2014

FWIW, I added this (though heavily modified) to upstream clang in r214410.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment