Skip to content

Commit 56313f6

Browse files
committed
[clang-format] Put peekNextToken(/*SkipComment=*/true) to good use
To prevent potential bugs in situations where we want to peek the next non-comment token. Differential Revision: https://reviews.llvm.org/D142412
1 parent 17c05a4 commit 56313f6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,9 @@ void UnwrappedLineParser::parseStructuralElement(
19061906
// declaration.
19071907
if (!IsTopLevel || !Style.isCpp() || !Previous || eof())
19081908
break;
1909-
if (isC78ParameterDecl(FormatTok, Tokens->peekNextToken(), Previous)) {
1909+
if (isC78ParameterDecl(FormatTok,
1910+
Tokens->peekNextToken(/*SkipComment=*/true),
1911+
Previous)) {
19101912
addUnwrappedLine();
19111913
return;
19121914
}
@@ -2376,7 +2378,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
23762378
if (FormatTok->is(tok::l_square))
23772379
return false;
23782380
if (FormatTok->is(tok::r_square)) {
2379-
const FormatToken *Next = Tokens->peekNextToken();
2381+
const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
23802382
if (Next->is(tok::greater))
23812383
return false;
23822384
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,13 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
34443444
"\n"
34453445
"private:\n"
34463446
" int c;\n"
3447+
"};\n"
3448+
"class B {\n"
3449+
"public:\n"
3450+
" std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3451+
"\n"
3452+
"private:\n"
3453+
" int c;\n"
34473454
"};");
34483455
}
34493456

@@ -9637,6 +9644,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
96379644
" return a;\n"
96389645
"}",
96399646
Style);
9647+
verifyFormat("byte *\n"
9648+
"f(a)\n"
9649+
"byte /* K&R C */ a[];\n"
9650+
"{\n"
9651+
" return a;\n"
9652+
"}\n"
9653+
"byte *\n"
9654+
"g(p)\n"
9655+
"byte /* K&R C */ *p;\n"
9656+
"{\n"
9657+
" return p;\n"
9658+
"}",
9659+
Style);
96409660
verifyFormat("bool f(int a, int) override;\n"
96419661
"Bar g(int a, Bar) final;\n"
96429662
"Bar h(a, Bar) final;",

0 commit comments

Comments
 (0)