<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Itanium ABI: debug assertion in template mangling with declype return"
   href="https://llvm.org/bugs/show_bug.cgi?id=31405">31405</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Itanium ABI: debug assertion in template mangling with declype return
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++14
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>spreis@yandex-team.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I develop in-house source code indexer and met the following issue.
The simple declaration cannot be mangled - Itanium mangler fails with assert if
clang is built in debug mode:

<span class="quote">>> cat test.cpp</span >
template <typename T> auto foo(T x) -> const decltype(x);

<span class="quote">>> indexer -std=c++14 test.cpp</span >

indexer: /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:3976:
void (anonymous namespace)::CXXNameMangler::mangleFunctionParam(const
clang::ParmVarDecl *): Assertion `parmDepth < FunctionTypeDepth.getDepth()'
failed.
Aborted (core dumped)

The problem is that both parts of comparison are '0'. This is due to the fact
that in this case CXXNameMangler::mangleFunctionParam() is called transitively
from CXXNameMangler::makeFunctionReturnTypeTags() (see stack trace below) and
for TrackReturnTypeTags mangler the FunctionTypeDepth is never increased in
this case. I believe this is incorrect: makeFunctionReturnTypeTags is called in
context of return value, so its FunctionTypeDepth should be 1 at minimum
(though I am not 100% sure: I am still new to clang).

The patch below fixes the issue for me:

~/LLVM/llvm/tools/clang/lib/AST$ svn diff
Index: ItaniumMangle.cpp
===================================================================
--- ItaniumMangle.cpp   (revision 289930)
+++ ItaniumMangle.cpp   (working copy)
@@ -4418,9 +4418,12 @@

   const FunctionProtoType *Proto =
       cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());

+  FunctionTypeDepthState saved = TrackReturnTypeTags.FunctionTypeDepth.push();
   TrackReturnTypeTags.FunctionTypeDepth.enterResultType();
   TrackReturnTypeTags.mangleType(Proto->getReturnType());
   TrackReturnTypeTags.FunctionTypeDepth.leaveResultType();
+  TrackReturnTypeTags.FunctionTypeDepth.pop(saved);

   return TrackReturnTypeTags.AbiTagsRoot.getSortedUniqueUsedAbiTags();
 }

Unfortunately I cannot provide full reproducer. I may try to create skeleton
visitor as reduced test case at later days. But maybe some C++ mangler capable
of handling this case already exists (c-index-test tool's mangling doesn't work
for function templates) - it should crash on debug CLANG with the declaration
above.

----   The stack trace: -------------------


<span class="quote">>> gdb --args indexer -std=c++14 test.cpp</span >
[...]
(gdb) run

indexer: /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:3976:
void (anonymous namespace)::CXXNameMangler::mangleFunctionParam(const
clang::ParmVarDecl *): Assertion `parmDepth < FunctionTypeDepth.getDepth()'
failed.

(gdb) bt
Program received signal SIGABRT, Aborted.
0x00007ffff69b8c37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

(gdb) bt

#0  0x00007ffff69b8c37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007ffff69bc028 in __GI_abort () at abort.c:89
#2  0x00007ffff69b1bf6 in __assert_fail_base () at assert.c:92
#3  0x00007ffff69b1ca2 in __GI___assert_fail () at assert.c:101
#4  0x0000000000e3600d in (anonymous
namespace)::CXXNameMangler::mangleFunctionParam ()
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:3976
#5  0x0000000000e2c9a2 in mangleType ()
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:3028
#6  (anonymous namespace)::CXXNameMangler::mangleType ()
    at /home/spreis/LLVM/llvm/tools/clang/include/clang/AST/TypeNodes.def:88
#7  0x0000000000e2bf95 in (anonymous namespace)::CXXNameMangler::mangleType ()
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:2252
#8  0x0000000000e2aa0f in makeFunctionReturnTypeTags ()  <<<<<<!!!!!!!!!
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:4424
#9  (anonymous namespace)::CXXNameMangler::mangleFunctionEncoding () 
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:652
#10 0x0000000000e28901 in (anonymous
namespace)::ItaniumMangleContextImpl::mangleCXXName ()
    at /home/spreis/LLVM/llvm/tools/clang/lib/AST/ItaniumMangle.cpp:4476
#11 0x000000000058fff0 in Collector::getMangledName ()
    at /home/spreis/work/indexer/collector.cpp:914
#12 0x000000000058d2f0 in Collector::registerFunctionDecl ()
    at /home/spreis/work/indexer/collector.cpp:640
#13 0x00000000005853c8 in BrowserASTVisitor::VisitFunctionDecl ()
    at /home/spreis/work/indexer/browserastvisitor.h:125
#14 0x0000000000573754 in WalkUpFromFunctionDecl ()
    at /home/spreis/include/clang/AST/DeclNodes.inc:371
#15 TraverseFunctionDecl (D=0x1c8acd0, this=<optimized out>)
    at /home/spreis/LLVM/build/include/clang/AST/RecursiveASTVisitor.h:1883
#16 clang::RecursiveASTVisitor<BrowserASTVisitor>::TraverseDecl () 
    at /place/home/spreis/LLVM/build/include/clang/AST/DeclNodes.inc:371
#17 0x00000000005736e2 in BrowserASTVisitor::TraverseDecl ()
    at     at /home/spreis/work/indexer/browserastvisitor.h:324
#18 0x0000000000574d5b in
clang::RecursiveASTVisitor<BrowserASTVisitor>::TraverseFunctionTemplateDecl ()
    at /home/spreis/LLVM/build/include/clang/AST/RecursiveASTVisitor.h:1637
....</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>