r214410 - Exposes a C API to name mangling for a given cursor.

Rafael Avila de Espindola rafael.espindola at gmail.com
Thu Jul 31 11:30:00 PDT 2014


This is the LLVM level name, right? For example, it doesn't include a leading underscore on Mach-o, correct?  Can you document that? Wouldn't it be more useful (and stable) to provide the fully mangled name?

Sent from my iPhone

> On Jul 31, 2014, at 14:04, Eli Bendersky <eliben at google.com> wrote:
> 
> Author: eliben
> Date: Thu Jul 31 13:04:56 2014
> New Revision: 214410
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=214410&view=rev
> Log:
> Exposes a C API to name mangling for a given cursor.
> 
> Inspired by 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.
> 
> 
> 
> Added:
>    cfe/trunk/test/Index/print-mangled-name.cpp
> Modified:
>    cfe/trunk/include/clang-c/Index.h
>    cfe/trunk/tools/c-index-test/c-index-test.c
>    cfe/trunk/tools/libclang/CIndex.cpp
>    cfe/trunk/tools/libclang/libclang.exports
> 
> Modified: cfe/trunk/include/clang-c/Index.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=214410&r1=214409&r2=214410&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang-c/Index.h (original)
> +++ cfe/trunk/include/clang-c/Index.h Thu Jul 31 13:04:56 2014
> @@ -3639,6 +3639,20 @@ CINDEX_LINKAGE CXString clang_Cursor_get
>  * @}
>  */
> 
> +/** \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);
> +
> +/**
> + * @}
> + */
> +
> /**
>  * \defgroup CINDEX_MODULE Module introspection
>  *
> 
> Added: cfe/trunk/test/Index/print-mangled-name.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-mangled-name.cpp?rev=214410&view=auto
> ==============================================================================
> --- cfe/trunk/test/Index/print-mangled-name.cpp (added)
> +++ cfe/trunk/test/Index/print-mangled-name.cpp Thu Jul 31 13:04:56 2014
> @@ -0,0 +1,23 @@
> +// 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
> 
> Modified: cfe/trunk/tools/c-index-test/c-index-test.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=214410&r1=214409&r2=214410&view=diff
> ==============================================================================
> --- cfe/trunk/tools/c-index-test/c-index-test.c (original)
> +++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Jul 31 13:04:56 2014
> @@ -1363,6 +1363,19 @@ static enum CXChildVisitResult PrintType
> }
> 
> /******************************************************************************/
> +/* 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.                                                          */
> /******************************************************************************/
> 
> @@ -4081,6 +4094,8 @@ int cindextest_main(int argc, const char
>   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);
> 
> Modified: cfe/trunk/tools/libclang/CIndex.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=214410&r1=214409&r2=214410&view=diff
> ==============================================================================
> --- cfe/trunk/tools/libclang/CIndex.cpp (original)
> +++ cfe/trunk/tools/libclang/CIndex.cpp Thu Jul 31 13:04:56 2014
> @@ -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,31 @@ CXSourceRange clang_Cursor_getSpellingNa
>   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);
> 
> Modified: cfe/trunk/tools/libclang/libclang.exports
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=214410&r1=214409&r2=214410&view=diff
> ==============================================================================
> --- cfe/trunk/tools/libclang/libclang.exports (original)
> +++ cfe/trunk/tools/libclang/libclang.exports Thu Jul 31 13:04:56 2014
> @@ -9,6 +9,7 @@ clang_CXXMethod_isVirtual
> 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
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list