parser heap-use-after-free

Hello,

[As with yesterday's report, I would have used your bugs.llvm.org
bug-reporting form, but have no account and still have not heard back
from bugs-admin@lists.llvm.org regarding my registration request]

This was a bit tricky even to find/reduce with a default clang build,
but once I built clang itself with ASAN, it's obvious and consistently
reproducible -- alternatively, set e.g., MALLOC_DEBUG_=45 (or any other
value in 1..255) to make it more consistently reproducible with the
non-ASAN binary.

Here's the minimized reproducer:

  printf 'template <int> void ngX() template z()->ngY<>;' | clang -cc1 -x c++

Here's most of the resulting output:

<stdin>:1:26: error: expected ';' at end of declaration
template <int> void ngX() template z()->ngY<>;
                         ^
                         ;
<stdin>:1:41: error: no template named 'ngY'; did you mean 'ngX'?
template <int> void ngX() template z()->ngY<>;
                                        ^~~
                                        ngX
<stdin>:1:21: note: 'ngX' declared here
template <int> void ngX() template z()->ngY<>;
                    ^
<stdin>:1:41: error: expected a type
template <int> void ngX() template z()->ngY<>;
                                        ^
<stdin>:1:41: error: variable cannot be defined in an explicit instantiation; if this declaration is meant to be a variable definition, remove the 'template' keyword
template <int> void ngX() template z()->ngY<>;
                          ~~~~~~~~~ ^
<stdin>:1:36: error: C++ requires a type specifier for all declarations
template <int> void ngX() template z()->ngY<>;
                                   ^
<stdin>:1:45: error: expected ';' at end of declaration
template <int> void ngX() template z()->ngY<>;
                                            ^
                                            ;

Thanks, filed as https://llvm.org/PR37008