[PATCH] D117398: [clang-format] Fix bug in parsing `operator<` with template

Jino Park via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 17 20:14:30 PST 2022


pjessesco updated this revision to Diff 400697.
pjessesco marked 2 inline comments as done.
pjessesco added a comment.

Fix to accept feedbacks.

1. `Forth` -> `Fourth`
2. Add unit test `verifyFormat("< <>");`
3. Fix to avoid undefined behavior


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117398/new/

https://reviews.llvm.org/D117398

Files:
  clang/lib/Format/FormatTokenLexer.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -9275,6 +9275,9 @@
   verifyFormat("operator SomeType<int>();");
   verifyFormat("operator SomeType<int, int>();");
   verifyFormat("operator SomeType<SomeType<int>>();");
+  verifyFormat("operator< <>();");
+  verifyFormat("< <>");
+  
   verifyFormat("void *operator new(std::size_t size);");
   verifyFormat("void *operator new[](std::size_t size);");
   verifyFormat("void operator delete(void *ptr);");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3345,6 +3345,9 @@
     if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
         !Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
       return true;
+    if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
+        Right.is(TT_TemplateOpener))
+      return true;
   } else if (Style.Language == FormatStyle::LK_Proto ||
              Style.Language == FormatStyle::LK_TextProto) {
     if (Right.is(tok::period) &&
Index: clang/lib/Format/FormatTokenLexer.cpp
===================================================================
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -429,11 +429,18 @@
   if (Tokens.size() < 3)
     return false;
 
+  auto First = Tokens.end() - 3;
   bool FourthTokenIsLess = false;
-  if (Tokens.size() > 3)
-    FourthTokenIsLess = (Tokens.end() - 4)[0]->is(tok::less);
 
-  auto First = Tokens.end() - 3;
+  if (Tokens.size() > 3) {
+    auto Fourth = (Tokens.end() - 4)[0];
+    FourthTokenIsLess = Fourth->is(tok::less);
+
+    // Do not remove a whitespace between the two "<" e.g. "operator< <>".
+    if (First[2]->is(tok::greater) && Fourth->is(tok::kw_operator))
+      return false;
+  }
+
   if (First[2]->is(tok::less) || First[1]->isNot(tok::less) ||
       First[0]->isNot(tok::less) || FourthTokenIsLess)
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117398.400697.patch
Type: text/x-patch
Size: 2135 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220118/f84bba39/attachment.bin>


More information about the cfe-commits mailing list