]> granicus.if.org Git - clang/commitdiff
[clang-format] Make parseUnaryOperator non-recursive, NFCI
authorKrasimir Georgiev <krasimir@google.com>
Wed, 1 Nov 2017 18:20:41 +0000 (18:20 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Wed, 1 Nov 2017 18:20:41 +0000 (18:20 +0000)
Summary:
This patch makes the implementation of parseUnaryOperator non-recursive. We had
a problem with a file starting with tens of thousands of +'es and -'es which
caused clang-format to stack overflow.

Reviewers: bkramer

Reviewed By: bkramer

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D39498

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317113 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp

index 1b22e266007094fcf10512bab5e62bc7311b846d..bc8fef8bda92e6ae25a1150b3632328ae7fdfeb0 100644 (file)
@@ -1662,17 +1662,15 @@ private:
   /// \brief Parse unary operator expressions and surround them with fake
   /// parentheses if appropriate.
   void parseUnaryOperator() {
-    if (!Current || Current->isNot(TT_UnaryOperator)) {
-      parse(PrecedenceArrowAndPeriod);
-      return;
+    llvm::SmallVector<FormatToken *, 2> Tokens;
+    while (Current && Current->is(TT_UnaryOperator)) {
+      Tokens.push_back(Current);
+      next();
     }
-
-    FormatToken *Start = Current;
-    next();
-    parseUnaryOperator();
-
-    // The actual precedence doesn't matter.
-    addFakeParenthesis(Start, prec::Unknown);
+    parse(PrecedenceArrowAndPeriod);
+    for (FormatToken *Token : llvm::reverse(Tokens))
+      // The actual precedence doesn't matter.
+      addFakeParenthesis(Token, prec::Unknown);
   }
 
   void parseConditionalExpr() {