r285934 - Fixed column shift when formatting line containing bit shift operators

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 3 09:57:30 PDT 2016


Author: malcolm.parsons
Date: Thu Nov  3 11:57:30 2016
New Revision: 285934

URL: http://llvm.org/viewvc/llvm-project?rev=285934&view=rev
Log:
Fixed column shift when formatting line containing bit shift operators

Summary:
During clang-format source lexing >> and << operators are split and
treated as two less/greater operators but column position of following
tokens was not adjusted accordingly.

Fixes PR26887

Patch by Paweł Żukowski.

Reviewers: djasper

Subscribers: malcolm.parsons, mprobst, klimek, cfe-commits

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

Modified:
    cfe/trunk/lib/Format/FormatTokenLexer.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/FormatTokenLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatTokenLexer.cpp?rev=285934&r1=285933&r2=285934&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatTokenLexer.cpp (original)
+++ cfe/trunk/lib/Format/FormatTokenLexer.cpp Thu Nov  3 11:57:30 2016
@@ -525,10 +525,12 @@ FormatToken *FormatTokenLexer::getNextTo
   } else if (FormatTok->Tok.is(tok::greatergreater)) {
     FormatTok->Tok.setKind(tok::greater);
     FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
+    ++Column;
     StateStack.push(LexerState::TOKEN_STASHED);
   } else if (FormatTok->Tok.is(tok::lessless)) {
     FormatTok->Tok.setKind(tok::less);
     FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
+    ++Column;
     StateStack.push(LexerState::TOKEN_STASHED);
   }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=285934&r1=285933&r2=285934&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Nov  3 11:57:30 2016
@@ -5501,6 +5501,18 @@ TEST_F(FormatTest, UnderstandsTemplatePa
   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
 }
 
+TEST_F(FormatTest, BitshiftOperatorWidth) {
+  EXPECT_EQ("int a = 1 << 2; /* foo\n"
+            "                   bar */",
+            format("int    a=1<<2;  /* foo\n"
+                   "                   bar */"));
+
+  EXPECT_EQ("int b = 256 >> 1; /* foo\n"
+            "                     bar */",
+            format("int  b  =256>>1 ;  /* foo\n"
+                   "                      bar */"));
+}
+
 TEST_F(FormatTest, UnderstandsBinaryOperators) {
   verifyFormat("COMPARE(a, ==, b);");
   verifyFormat("auto s = sizeof...(Ts) - 1;");




More information about the cfe-commits mailing list