fix PR4452, a crash on invalid.  The error recovery is still terrible in this case
but at least we don't crash :)


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74264 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp
index ab359f4..57a09fb 100644
--- a/lib/Parse/ParseTemplate.cpp
+++ b/lib/Parse/ParseTemplate.cpp
@@ -621,7 +621,11 @@
 /// replaced with a type annotation token. Otherwise, the
 /// simple-template-id is always replaced with a template-id
 /// annotation token.
-void Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
+///
+/// If an unrecoverable parse error occurs and no annotation token can be
+/// formed, this function returns true.
+///
+bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
                                      const CXXScopeSpec *SS, 
                                      SourceLocation TemplateKWLoc,
                                      bool AllowTypeAnnotation) {
@@ -644,14 +648,19 @@
                                                   TemplateArgIsType,
                                                   TemplateArgLocations,
                                                   RAngleLoc);
+  
+  if (Invalid) {
+    // If we failed to parse the template ID but skipped ahead to a >, we're not
+    // going to be able to form a token annotation.  Eat the '>' if present.
+    if (Tok.is(tok::greater))
+      ConsumeToken();
+    return true;
+  }
 
   ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(),
                                      TemplateArgIsType.data(),
                                      TemplateArgs.size());
 
-  if (Invalid) // FIXME: How to recover from a broken template-id?
-    return; 
-
   // Build the annotation token.
   if (TNK == TNK_Type_template && AllowTypeAnnotation) {
     Action::TypeResult Type 
@@ -659,8 +668,13 @@
                                     LAngleLoc, TemplateArgsPtr,
                                     &TemplateArgLocations[0],
                                     RAngleLoc);
-    if (Type.isInvalid()) // FIXME: better recovery?
-      return;
+    if (Type.isInvalid()) {
+      // If we failed to parse the template ID but skipped ahead to a >, we're not
+      // going to be able to form a token annotation.  Eat the '>' if present.
+      if (Tok.is(tok::greater))
+        ConsumeToken();
+      return true;
+    }
 
     Tok.setKind(tok::annot_typename);
     Tok.setAnnotationValue(Type.get());
@@ -705,6 +719,7 @@
   // In case the tokens were cached, have Preprocessor replace them with the
   // annotation token.
   PP.AnnotateCachedTokens(Tok);
+  return false;
 }
 
 /// \brief Replaces a template-id annotation token with a type