From 6af3f14efb0ad169eb5239ca1a9b7027925aadd2 Mon Sep 17 00:00:00 2001 From: Malcolm Parsons Date: Thu, 3 Nov 2016 16:57:30 +0000 Subject: [PATCH] Fixed column shift when formatting line containing bit shift operators MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 llvm-svn: 285934 --- clang/lib/Format/FormatTokenLexer.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index c9670ae..2aa48e3 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -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); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5c70b8c..7ba93a7 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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;"); -- 2.7.4