[PATCH] D79293: [clang-format] [PR45218] Fix an issue where < and > and >> in a for loop gets incorrectly interpreted at a TemplateOpener/Closer

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 4 01:01:40 PDT 2020


MyDeveloperDay updated this revision to Diff 261748.
MyDeveloperDay marked 3 inline comments as done.
MyDeveloperDay added a comment.

Remove tok::semi check as its not actually needed now we label the < and > correctly


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

https://reviews.llvm.org/D79293

Files:
  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
@@ -7065,6 +7065,21 @@
   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
+
+  verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
+}
+
+TEST_F(FormatTest, UnderstandsShiftOperators) {
+  verifyFormat("if (i < x >> 1)");
+  verifyFormat("while (i < x >> 1)");
+  verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
+  verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
+  verifyFormat(
+      "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
+  verifyFormat("Foo.call<Bar<Function>>()");
+  verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
+  verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
+               "++i, v = v >> 1)");
 }
 
 TEST_F(FormatTest, BitshiftOperatorWidth) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -119,7 +119,16 @@
             (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
              Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
           CurrentToken->Type = TT_DictLiteral;
-        else
+        // In if/for/while  (i < x >> 1) ensure we don't see the <> as
+        // TemplateOpener/Closer
+        else if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
+                 CurrentToken->Next->Next &&
+                 !CurrentToken->Next->Next->isOneOf(tok::l_paren,
+                                                    tok::coloncolon) &&
+                 Line.First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_while)) {
+          Left->Type = TT_BinaryOperator;
+          return false;
+        } else
           CurrentToken->Type = TT_TemplateCloser;
         next();
         return true;
@@ -1522,8 +1531,8 @@
     if (Style.Language == FormatStyle::LK_JavaScript) {
       if (Current.is(tok::exclaim)) {
         if (Current.Previous &&
-            (Keywords.IsJavaScriptIdentifier(
-                 *Current.Previous, /* AcceptIdentifierName= */ true) ||
+            (Keywords.IsJavaScriptIdentifier(*Current.Previous,
+                                             /*AcceptIdentifierName=*/true) ||
              Current.Previous->isOneOf(
                  tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace,
                  Keywords.kw_type, Keywords.kw_get, Keywords.kw_set) ||
@@ -2174,7 +2183,7 @@
   FormatToken *Current;
 };
 
-} // end anonymous namespace
+} // namespace
 
 void TokenAnnotator::setCommentLineLevels(
     SmallVectorImpl<AnnotatedLine *> &Lines) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79293.261748.patch
Type: text/x-patch
Size: 3013 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200504/fd6a338e/attachment.bin>


More information about the cfe-commits mailing list