Skip to content

Commit 8fa56f7

Browse files
penagoskrasimirgg
authored andcommitted
[clang-format] Prevent extraneous space insertion in bitshift operators
This serves to augment the improvements made in https://reviews.llvm.org/D86581. It prevents clang-format from interpreting bitshift operators as template arguments in certain circumstances. This is an attempt at fixing https://bugs.llvm.org/show_bug.cgi?id=49868 Reviewed By: MyDeveloperDay, krasimir Differential Revision: https://reviews.llvm.org/D100778
1 parent fb92cf9 commit 8fa56f7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ class AnnotatingParser {
116116
while (CurrentToken) {
117117
if (CurrentToken->is(tok::greater)) {
118118
// Try to do a better job at looking for ">>" within the condition of
119-
// a statement.
119+
// a statement. Conservatively insert spaces between consecutive ">"
120+
// tokens to prevent splitting right bitshift operators and potentially
121+
// altering program semantics. This check is overly conservative and
122+
// will prevent spaces from being inserted in select nested template
123+
// parameter cases, but should not alter program semantics.
120124
if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
121125
Left->ParentBracket != tok::less &&
122-
isKeywordWithCondition(*Line.First))
126+
(isKeywordWithCondition(*Line.First) ||
127+
CurrentToken->getStartOfNonWhitespace() ==
128+
CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
129+
-1)))
123130
return false;
124131
Left->MatchingParen = CurrentToken;
125132
CurrentToken->MatchingParen = Left;

clang/unittests/Format/FormatTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8007,6 +8007,8 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
80078007
verifyFormat("a<int> = 1;", Style);
80088008
verifyFormat("a<int> >>= 1;", Style);
80098009

8010+
verifyFormat("test < a | b >> c;");
8011+
verifyFormat("test<test<a | b>> c;");
80108012
verifyFormat("test >> a >> b;");
80118013
verifyFormat("test << a >> b;");
80128014

0 commit comments

Comments
 (0)