]> granicus.if.org Git - clang/commitdiff
[clang-format] Recognize Java logical shift assignment operator
authorNico Weber <nicolasweber@gmx.de>
Tue, 11 Apr 2017 15:50:04 +0000 (15:50 +0000)
committerNico Weber <nicolasweber@gmx.de>
Tue, 11 Apr 2017 15:50:04 +0000 (15:50 +0000)
At present, clang-format mangles Java containing logical right shift operators
('>>>=' or '>>>'), splitting them in two, resulting in invalid code:

 public class Minimal {
   public void func(String args) {
     int i = 42;
-    i >>>= 1;
+    i >> >= 1;
     return i;
   }
 }

This adds both forms of logical right shift to the FormatTokenLexer, so
clang-format won't attempt to split them and insert bogus whitespace.

https://reviews.llvm.org/D31652
Patch from Richard Bradfield <bradfier@fstab.me>!

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

lib/Format/FormatTokenLexer.cpp
unittests/Format/FormatTestJava.cpp

index 9415dbe9ab340e5e2ae15ea1e1412d38b67ca992..4ee43d6937e00866b10d1dcff180afadf9859bb4 100644 (file)
@@ -84,6 +84,19 @@ void FormatTokenLexer::tryMergePreviousTokens() {
     if (tryMergeTokens(JSRightArrow, TT_JsFatArrow))
       return;
   }
+
+  if (Style.Language == FormatStyle::LK_Java) {
+    static const tok::TokenKind JavaRightLogicalShift[] = {tok::greater,
+                                                           tok::greater,
+                                                           tok::greater};
+    static const tok::TokenKind JavaRightLogicalShiftAssign[] = {tok::greater,
+                                                                 tok::greater,
+                                                                 tok::greaterequal};
+    if (tryMergeTokens(JavaRightLogicalShift, TT_BinaryOperator))
+      return;
+    if (tryMergeTokens(JavaRightLogicalShiftAssign, TT_BinaryOperator))
+      return;
+  }
 }
 
 bool FormatTokenLexer::tryMergeNSStringLiteral() {
index ee09ca940f145fde241363a1f32ee776b244d382..6e685f6703e172bd0f9095d685f921834b22b7d3 100644 (file)
@@ -522,5 +522,17 @@ TEST_F(FormatTestJava, AlignsBlockComments) {
                    "  void f() {}"));
 }
 
+TEST_F(FormatTestJava, RetainsLogicalShifts) {
+    verifyFormat("void f() {\n"
+                 "  int a = 1;\n"
+                 "  a >>>= 1;\n"
+                 "}");
+    verifyFormat("void f() {\n"
+                 "  int a = 1;\n"
+                 "  a = a >>> 1;\n"
+                 "}");
+}
+
+
 } // end namespace tooling
 } // end namespace clang