<div dir="ltr">And valgrind agrees: <div><br></div><div><div>valgrind  --leak-check=full c-index-test -test-print-mangle tools/clang/test/Index/Output/print-mangled-name.cpp.tmp_linux.ast</div></div><div><div>==1055== 30 bytes in 3 blocks are definitely lost in loss record 16 of 29</div>
<div>==1055==    at 0x4C2CF27: malloc (vg_replace_malloc.c:291)</div><div>==1055==    by 0x4F668A4: clang::cxstring::createDup(llvm::StringRef) (in lib/libclang.so.3.5)</div><div>==1055==    by 0x4F2C894: clang_Cursor_getMangling (in lib/libclang.so.3.5)</div>
<div>==1055==    by 0x40B86B: PrintMangledName (in bin/c-index-test)</div><div>==1055==    by 0x4F39AA8: clang::cxcursor::CursorVisitor::Visit(CXCursor, bool) (in lib/libclang.so.3.5)</div><div>==1055==    by 0x4F3ACAF: clang::cxcursor::CursorVisitor::VisitDeclContext(clang::DeclContext*) (in lib/libclang.so.3.5)</div>
<div>==1055==    by 0x4F39810: clang::cxcursor::CursorVisitor::VisitChildren(CXCursor) (in lib/libclang.so.3.5)</div><div>==1055==    by 0x4F42DF2: clang_visitChildren (in lib/libclang.so.3.5)</div><div>==1055==    by 0x40FFB9: perform_test_load.isra.7 (in bin/c-index-test)</div>
<div>==1055==    by 0x41020C: perform_test_load_tu (in bin/c-index-test)</div><div>==1055==    by 0x414BF6: cindextest_main (in bin/c-index-test)</div><div>==1055==    by 0x4158AE: thread_runner (in bin/c-index-test)</div>
<div><br></div><div>~                                                                                   </div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Aug 1, 2014 at 3:51 PM, Kostya Serebryany <span dir="ltr"><<a href="mailto:kcc@google.com" target="_blank">kcc@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">We observe a new leak on the sanitizer bot: <div><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/4103/steps/check-clang%20asan/logs/stdio" target="_blank">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/4103/steps/check-clang%20asan/logs/stdio</a><br>


</div><div>It seems to be caused by this change. </div><div><pre style="font-family:'Courier New',courier,monotype,monospace;color:rgb(0,0,0);font-size:medium"><span>Direct leak of 30 byte(s) in 3 object(s) allocated from:
    #0 0x49a509 in malloc /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:40
    #1 0x7f9b1b401cd8 in clang::cxstring::createDup(llvm::StringRef) /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CXString.cpp:100:40
    #2 0x7f9b1b3a323d in clang_Cursor_getMangling /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CIndex.cpp:3693:10
    #3 0x4c5233 in PrintMangledName /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/c-index-test/c-index-test.c:1373:17
    #4 0x7f9b1b36ff52 in clang::cxcursor::CursorVisitor::Visit(CXCursor, bool) /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CIndex.cpp:207:11
    #5 0x7f9b1b376da2 in clang::cxcursor::CursorVisitor::VisitDeclContext(clang::DeclContext*) /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CIndex.cpp:654:9
    #6 0x7f9b1b3710ed in clang::cxcursor::CursorVisitor::VisitChildren(CXCursor) /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CIndex.cpp:529:20
    #7 0x7f9b1b39ca2e in clang_visitChildren /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/libclang/CIndex.cpp:3364:10
    #8 0x4ba911 in perform_test_load /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/c-index-test/c-index-test.c:1437:5
    #9 0x4ba3d0 in perform_test_load_tu /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/c-index-test/c-index-test.c:1468:12
    #10 0x4c2c1a in cindextest_main /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/c-index-test/c-index-test.c:4052:14
    #11 0x4c52a7 in thread_runner /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/tools/clang/tools/c-index-test/c-index-test.c:4133:25
    #12 0x7f9b1cef555f in ExecuteOnThread_Dispatch(void*) /home/dtoolsbot/build/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Support/Threading.cpp:40:3
    #13 0x7f9b1a537181 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x8181)

SUMMARY: AddressSanitizer: 30 byte(s) leaked in 3 allocation(s).
</span></pre></div><div><span><br></span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jul 31, 2014 at 10:04 PM, Eli Bendersky <span dir="ltr"><<a href="mailto:eliben@google.com" target="_blank">eliben@google.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: eliben<br>
Date: Thu Jul 31 13:04:56 2014<br>
New Revision: 214410<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=214410&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=214410&view=rev</a><br>
Log:<br>
Exposes a C API to name mangling for a given cursor.<br>
<br>
Inspired by <a href="https://gist.github.com/tritao/2766291" target="_blank">https://gist.github.com/tritao/2766291</a>, and was previously discussed<br>
on cfe-dev: <a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-June/037577.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-June/037577.html</a><br>
<br>
Adding testing capability via c-index-test.<br>
<br>
<br>
<br>
Added:<br>
    cfe/trunk/test/Index/print-mangled-name.cpp<br>
Modified:<br>
    cfe/trunk/include/clang-c/Index.h<br>
    cfe/trunk/tools/c-index-test/c-index-test.c<br>
    cfe/trunk/tools/libclang/CIndex.cpp<br>
    cfe/trunk/tools/libclang/libclang.exports<br>
<br>
Modified: cfe/trunk/include/clang-c/Index.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=214410&r1=214409&r2=214410&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=214410&r1=214409&r2=214410&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/include/clang-c/Index.h (original)<br>
+++ cfe/trunk/include/clang-c/Index.h Thu Jul 31 13:04:56 2014<br>
@@ -3639,6 +3639,20 @@ CINDEX_LINKAGE CXString clang_Cursor_get<br>
  * @}<br>
  */<br>
<br>
+/** \defgroup CINDEX_MANGLE Name Mangling API Functions<br>
+ *<br>
+ * @{<br>
+ */<br>
+<br>
+/**<br>
+ * \brief Retrieve the CXString representing the mangled name of the cursor.<br>
+ */<br>
+CINDEX_LINKAGE CXString clang_Cursor_getMangling(CXCursor);<br>
+<br>
+/**<br>
+ * @}<br>
+ */<br>
+<br>
 /**<br>
  * \defgroup CINDEX_MODULE Module introspection<br>
  *<br>
<br>
Added: cfe/trunk/test/Index/print-mangled-name.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-mangled-name.cpp?rev=214410&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-mangled-name.cpp?rev=214410&view=auto</a><br>


==============================================================================<br>
--- cfe/trunk/test/Index/print-mangled-name.cpp (added)<br>
+++ cfe/trunk/test/Index/print-mangled-name.cpp Thu Jul 31 13:04:56 2014<br>
@@ -0,0 +1,23 @@<br>
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu -emit-pch %s -o %t_linux.ast<br>
+// RUN: c-index-test -test-print-mangle %t_linux.ast | FileCheck %s --check-prefix=ITANIUM<br>
+<br>
+// RUN: %clang_cc1 -triple i686-pc-win32 -emit-pch %s -o %t_msft.ast<br>
+// RUN: c-index-test -test-print-mangle %t_msft.ast | FileCheck %s --check-prefix=MICROSOFT<br>
+<br>
+int foo(int, int);<br>
+// ITANIUM: mangled=_Z3fooii<br>
+// MICROSOFT: mangled=?foo@@YAHHH<br>
+<br>
+int foo(float, int);<br>
+// ITANIUM: mangled=_Z3foofi<br>
+// MICROSOFT: mangled=?foo@@YAHMH<br>
+<br>
+struct S {<br>
+  int x, y;<br>
+};<br>
+// ITANIUM: StructDecl{{.*}}mangled=]<br>
+// MICROSOFT: StructDecl{{.*}}mangled=]<br>
+<br>
+int foo(S, S&);<br>
+// ITANIUM: mangled=_Z3foo1SRS<br>
+// MICROSOFT: mangled=?foo@@YAHUS<br>
<br>
Modified: cfe/trunk/tools/c-index-test/c-index-test.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=214410&r1=214409&r2=214410&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=214410&r1=214409&r2=214410&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)<br>
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Jul 31 13:04:56 2014<br>
@@ -1363,6 +1363,19 @@ static enum CXChildVisitResult PrintType<br>
 }<br>
<br>
 /******************************************************************************/<br>
+/* Mangling testing.                                                          */<br>
+/******************************************************************************/<br>
+<br>
+static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,<br>
+                                                CXClientData d) {<br>
+  CXString MangledName;<br>
+  PrintCursor(cursor, NULL);<br>
+  MangledName = clang_Cursor_getMangling(cursor);<br>
+  printf(" [mangled=%s]\n", clang_getCString(MangledName));<br>
+  return CXChildVisit_Continue;<br>
+}<br>
+<br>
+/******************************************************************************/<br>
 /* Bitwidth testing.                                                          */<br>
 /******************************************************************************/<br>
<br>
@@ -4081,6 +4094,8 @@ int cindextest_main(int argc, const char<br>
   else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)<br>
     return perform_test_load_source(argc - 2, argv + 2, "all",<br>
                                     PrintBitWidth, 0);<br>
+  else if (argc > 2 && strcmp(argv[1], "-test-print-mangle") == 0)<br>
+    return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);<br>
   else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {<br>
     if (argc > 2)<br>
       return print_usrs(argv + 2, argv + argc);<br>
<br>
Modified: cfe/trunk/tools/libclang/CIndex.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=214410&r1=214409&r2=214410&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=214410&r1=214409&r2=214410&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/tools/libclang/CIndex.cpp (original)<br>
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Jul 31 13:04:56 2014<br>
@@ -22,6 +22,7 @@<br>
 #include "CXType.h"<br>
 #include "CursorVisitor.h"<br>
 #include "clang/AST/Attr.h"<br>
+#include "clang/AST/Mangle.h"<br>
 #include "clang/AST/StmtVisitor.h"<br>
 #include "clang/Basic/Diagnostic.h"<br>
 #include "clang/Basic/DiagnosticCategories.h"<br>
@@ -3667,6 +3668,31 @@ CXSourceRange clang_Cursor_getSpellingNa<br>
   return cxloc::translateSourceRange(Ctx, Loc);<br>
 }<br>
<br>
+CXString clang_Cursor_getMangling(CXCursor C) {<br>
+  if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind))<br>
+    return cxstring::createEmpty();<br>
+<br>
+  const Decl *D = getCursorDecl(C);<br>
+  // Mangling only works for functions and variables.<br>
+  if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))<br>
+    return cxstring::createEmpty();<br>
+<br>
+  const NamedDecl *ND = cast<NamedDecl>(D);<br>
+  std::unique_ptr<MangleContext> MC(ND->getASTContext().createMangleContext());<br>
+<br>
+  std::string Buf;<br>
+  llvm::raw_string_ostream OS(Buf);<br>
+  MC->mangleName(ND, OS);<br>
+  OS.flush();<br>
+<br>
+  // The Microsoft mangler may insert a special character in the beginning to<br>
+  // prevent further mangling. We can strip that for display purposes.<br>
+  if (Buf[0] == '\x01') {<br>
+    Buf.erase(0, 1);<br>
+  }<br>
+  return cxstring::createDup(Buf);<br>
+}<br>
+<br>
 CXString clang_getCursorDisplayName(CXCursor C) {<br>
   if (!clang_isDeclaration(C.kind))<br>
     return clang_getCursorSpelling(C);<br>
<br>
Modified: cfe/trunk/tools/libclang/libclang.exports<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=214410&r1=214409&r2=214410&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=214410&r1=214409&r2=214410&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/tools/libclang/libclang.exports (original)<br>
+++ cfe/trunk/tools/libclang/libclang.exports Thu Jul 31 13:04:56 2014<br>
@@ -9,6 +9,7 @@ clang_CXXMethod_isVirtual<br>
 clang_Cursor_getArgument<br>
 clang_Cursor_getBriefCommentText<br>
 clang_Cursor_getCommentRange<br>
+clang_Cursor_getMangling<br>
 clang_Cursor_getParsedComment<br>
 clang_Cursor_getRawCommentText<br>
 clang_Cursor_getNumArguments<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>