[OPENMP] Fix for parsing OpenMP directives with extra braces, brackets and parens

llvm-svn: 197553
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 79a02ee..2dc3d2c 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -159,7 +159,8 @@
 /// SkipToTok is specified, it calls SkipUntil(SkipToTok).  Finally, true is
 /// returned.
 bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
-                              const char *Msg, tok::TokenKind SkipToTok) {
+                              const char *Msg, tok::TokenKind SkipToTok,
+                              bool NoCount) {
   if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
     ConsumeAnyToken();
     return false;
@@ -189,8 +190,12 @@
   } else
     Diag(Tok, DiagID) << Msg;
 
-  if (SkipToTok != tok::unknown)
-    SkipUntil(SkipToTok, StopAtSemi);
+  if (SkipToTok != tok::unknown) {
+    SkipUntilFlags Flags = StopAtSemi;
+    if (NoCount)
+      Flags = Flags | NoBracketsCount;
+    SkipUntil(SkipToTok, Flags);
+  }
   return true;
 }
 
@@ -314,26 +319,32 @@
     case tok::l_paren:
       // Recursively skip properly-nested parens.
       ConsumeParen();
-      if (HasFlagsSet(Flags, StopAtCodeCompletion))
-        SkipUntil(tok::r_paren, StopAtCodeCompletion);
-      else
-        SkipUntil(tok::r_paren);
+      if (!HasFlagsSet(Flags, NoBracketsCount)) {
+        if (HasFlagsSet(Flags, StopAtCodeCompletion))
+          SkipUntil(tok::r_paren, StopAtCodeCompletion);
+        else
+          SkipUntil(tok::r_paren);
+      }
       break;
     case tok::l_square:
       // Recursively skip properly-nested square brackets.
       ConsumeBracket();
-      if (HasFlagsSet(Flags, StopAtCodeCompletion))
-        SkipUntil(tok::r_square, StopAtCodeCompletion);
-      else
-        SkipUntil(tok::r_square);
+      if (!HasFlagsSet(Flags, NoBracketsCount)) {
+        if (HasFlagsSet(Flags, StopAtCodeCompletion))
+          SkipUntil(tok::r_square, StopAtCodeCompletion);
+        else
+          SkipUntil(tok::r_square);
+      }
       break;
     case tok::l_brace:
       // Recursively skip properly-nested braces.
       ConsumeBrace();
-      if (HasFlagsSet(Flags, StopAtCodeCompletion))
-        SkipUntil(tok::r_brace, StopAtCodeCompletion);
-      else
-        SkipUntil(tok::r_brace);
+      if (!HasFlagsSet(Flags, NoBracketsCount)) {
+        if (HasFlagsSet(Flags, StopAtCodeCompletion))
+          SkipUntil(tok::r_brace, StopAtCodeCompletion);
+        else
+          SkipUntil(tok::r_brace);
+      }
       break;
 
     // Okay, we found a ']' or '}' or ')', which we think should be balanced.
@@ -342,17 +353,17 @@
     // higher level, we will assume that this matches the unbalanced token
     // and return it.  Otherwise, this is a spurious RHS token, which we skip.
     case tok::r_paren:
-      if (ParenCount && !isFirstTokenSkipped)
+      if (!HasFlagsSet(Flags, NoBracketsCount) && ParenCount && !isFirstTokenSkipped)
         return false;  // Matches something.
       ConsumeParen();
       break;
     case tok::r_square:
-      if (BracketCount && !isFirstTokenSkipped)
+      if (!HasFlagsSet(Flags, NoBracketsCount) && BracketCount && !isFirstTokenSkipped)
         return false;  // Matches something.
       ConsumeBracket();
       break;
     case tok::r_brace:
-      if (BraceCount && !isFirstTokenSkipped)
+      if (!HasFlagsSet(Flags, NoBracketsCount) && BraceCount && !isFirstTokenSkipped)
         return false;  // Matches something.
       ConsumeBrace();
       break;
@@ -2031,7 +2042,7 @@
                                             const char *Msg,
                                             tok::TokenKind SkipToToc ) {
   LOpen = P.Tok.getLocation();
-  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
+  if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc, NoCount))
     return true;
   
   if (getDepth() < MaxDepth)
@@ -2056,16 +2067,21 @@
 
   // If we're not already at some kind of closing bracket, skip to our closing
   // token.
+  Parser::SkipUntilFlags Flags = Parser::StopAtSemi | Parser::StopBeforeMatch;
+  if (NoCount)
+    Flags = Flags | Parser::NoBracketsCount;
   if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
       P.Tok.isNot(tok::r_square) &&
-      P.SkipUntil(Close, FinalToken,
-                  Parser::StopAtSemi | Parser::StopBeforeMatch) &&
+      P.SkipUntil(Close, FinalToken, Flags) &&
       P.Tok.is(Close))
     LClose = P.ConsumeAnyToken();
   return true;
 }
 
 void BalancedDelimiterTracker::skipToEnd() {
-  P.SkipUntil(Close, Parser::StopBeforeMatch);
+  Parser::SkipUntilFlags Flags = Parser::StopBeforeMatch;
+  if (NoCount)
+    Flags = Flags | Parser::NoBracketsCount;
+  P.SkipUntil(Close, Flags);
   consumeClose();
 }