]> granicus.if.org Git - clang/commitdiff
Fixed column shift when formatting line containing bit shift operators
authorMalcolm Parsons <malcolm.parsons@gmail.com>
Thu, 3 Nov 2016 16:57:30 +0000 (16:57 +0000)
committerMalcolm Parsons <malcolm.parsons@gmail.com>
Thu, 3 Nov 2016 16:57:30 +0000 (16:57 +0000)
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

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

lib/Format/FormatTokenLexer.cpp
unittests/Format/FormatTest.cpp

index c9670aeff66258442c166aef16df80247138b759..2aa48e3f49136f090d2ddab1146eaf86455381f3 100644 (file)
@@ -525,10 +525,12 @@ FormatToken *FormatTokenLexer::getNextToken() {
   } 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);
   }
 
index 5c70b8c12bbbb897e03f3ce1d23a369d206dab1c..7ba93a7f9e04a0faa6422181fa4ef724ca1e1394 100644 (file)
@@ -5501,6 +5501,18 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
   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;");