Streamline BalancedDelimiterTracker, by eliminating the duplicate
paren/brace/bracket tracking (the Consume* functions already did it),
removing the use of ConsumeAnyToken(), and moving the hot paths inline
with the error paths out-of-line.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152274 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParseCXXInlineMethods.cpp b/lib/Parse/ParseCXXInlineMethods.cpp
index 8cec31c..6fe33f4 100644
--- a/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/lib/Parse/ParseCXXInlineMethods.cpp
@@ -193,7 +193,7 @@
   tok::TokenKind kind = Tok.getKind();
   if (kind == tok::equal) {
     Toks.push_back(Tok);
-    ConsumeAnyToken();
+    ConsumeToken();
   }
 
   if (kind == tok::l_brace) {
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp
index 3a261f0..e3d31e3 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -818,7 +818,7 @@
     if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
                                                         OrigLoc))
     while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
-        ConsumeAnyToken();
+      ConsumeAnyToken();
   }
 }
 
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp
index a1c3b05..669b5b8 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -1318,22 +1318,27 @@
       
       Expr *ExecConfig = 0;
 
-      BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
       BalancedDelimiterTracker PT(*this, tok::l_paren);
 
       if (OpKind == tok::lesslessless) {
         ExprVector ExecConfigExprs(Actions);
         CommaLocsTy ExecConfigCommaLocs;
-        LLLT.consumeOpen();
+        SourceLocation OpenLoc = ConsumeToken();
 
         if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
           LHS = ExprError();
         }
 
-        if (LHS.isInvalid()) {
+        SourceLocation CloseLoc = Tok.getLocation();
+        if (Tok.is(tok::greatergreatergreater)) {
+          ConsumeToken();
+        } else if (LHS.isInvalid()) {
           SkipUntil(tok::greatergreatergreater);
-        } else if (LLLT.consumeClose()) {
+        } else {
           // There was an error closing the brackets
+          Diag(Tok, diag::err_expected_ggg);
+          Diag(OpenLoc, diag::note_matching) << "<<<";
+          SkipUntil(tok::greatergreatergreater);
           LHS = ExprError();
         }
 
@@ -1346,9 +1351,9 @@
 
         if (!LHS.isInvalid()) {
           ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
-                                    LLLT.getOpenLocation(), 
+                                    OpenLoc, 
                                     move_arg(ExecConfigExprs), 
-                                    LLLT.getCloseLocation());
+                                    CloseLoc);
           if (ECResult.isInvalid())
             LHS = ExprError();
           else
diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp
index 971c7d4..3309b84 100644
--- a/lib/Parse/ParseObjc.cpp
+++ b/lib/Parse/ParseObjc.cpp
@@ -1192,7 +1192,7 @@
     return true;
   }
 
-  EndLoc = ConsumeAnyToken();
+  EndLoc = ConsumeToken();
 
   // Convert the list of protocols identifiers into a list of protocol decls.
   Actions.FindProtocolDeclaration(WarnOnDeclarations,
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index 8b1765d..725a8f8 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -1651,68 +1651,43 @@
   return Actions.ConvertDeclToDeclGroup(Import.get());
 }
 
-bool Parser::BalancedDelimiterTracker::consumeOpen() {
-  // Try to consume the token we are holding
-  if (P.Tok.is(Kind)) {
-    P.QuantityTracker.push(Kind);
-    Cleanup = true;
-    if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
-      LOpen = P.ConsumeAnyToken();
-      return false;
-    } else {
-      P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
-      P.SkipUntil(tok::eof);
-    }
-  }
-  return true;
+bool Parser::BalancedDelimiterTracker::diagnoseOverflow() {
+  P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
+  P.SkipUntil(tok::eof);
+  return true;  
 }
 
 bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID, 
                                             const char *Msg,
                                             tok::TokenKind SkipToToc ) {
   LOpen = P.Tok.getLocation();
-  if (!P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc)) {
-    P.QuantityTracker.push(Kind);
-    Cleanup = true;
-    if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
-      return false;
-    } else {
-      P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
-      P.SkipUntil(tok::eof);
-    }
-  }
-  return true;
+  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
+    return true;
+  
+  if (getDepth() < MaxDepth)
+    return false;
+    
+  return diagnoseOverflow();
 }
 
-bool Parser::BalancedDelimiterTracker::consumeClose() {
-  if (P.Tok.is(Close)) {
-    LClose = P.ConsumeAnyToken();
-    if (Cleanup)
-      P.QuantityTracker.pop(Kind);
-
-    Cleanup = false;
-    return false;
-  } else {
-    const char *LHSName = "unknown";
-    diag::kind DID = diag::err_parse_error;
-    switch (Close) {
-    default: break;
-    case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
-    case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
-    case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
-    case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
-    case tok::greatergreatergreater:
-                        LHSName = "<<<"; DID = diag::err_expected_ggg; break;
-    }
-    P.Diag(P.Tok, DID);
-    P.Diag(LOpen, diag::note_matching) << LHSName;
-    if (P.SkipUntil(Close))
-      LClose = P.Tok.getLocation();
+bool Parser::BalancedDelimiterTracker::diagnoseMissingClose() {
+  assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
+  
+  const char *LHSName = "unknown";
+  diag::kind DID = diag::err_parse_error;
+  switch (Close) {
+  default: break;
+  case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
+  case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
+  case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
   }
+  P.Diag(P.Tok, DID);
+  P.Diag(LOpen, diag::note_matching) << LHSName;
+  if (P.SkipUntil(Close))
+    LClose = P.Tok.getLocation();
   return true;
 }
 
 void Parser::BalancedDelimiterTracker::skipToEnd() {
   P.SkipUntil(Close, false);
-  Cleanup = false;
 }