From: Malcolm Parsons Date: Thu, 3 Nov 2016 16:57:30 +0000 (+0000) Subject: Fixed column shift when formatting line containing bit shift operators X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=13730b3c9ca57a552cdaa361132318a162316c05;p=clang 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285934 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatTokenLexer.cpp b/lib/Format/FormatTokenLexer.cpp index c9670aeff6..2aa48e3f49 100644 --- a/lib/Format/FormatTokenLexer.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5c70b8c12b..7ba93a7f9e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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;");