Skip to content

Commit 3063153

Browse files
authored
[clang-format] Fix a bug in annotating ObjCMethodSpecifier (#127159)
Fixes #58202.
1 parent 5c93eb5 commit 3063153

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ class AnnotatingParser {
13131313
switch (bool IsIf = false; Tok->Tok.getKind()) {
13141314
case tok::plus:
13151315
case tok::minus:
1316-
if (!Tok->Previous && Line.MustBeDeclaration)
1316+
if (!Tok->getPreviousNonComment() && Line.MustBeDeclaration)
13171317
Tok->setType(TT_ObjCMethodSpecifier);
13181318
break;
13191319
case tok::colon:

clang/unittests/Format/FormatTestObjC.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
567567
" error:(NSError **)theError {\n"
568568
"}");
569569
verifyFormat("+ (instancetype)new;");
570+
571+
verifyFormat("/*\n"
572+
" */\n"
573+
"- (void)foo;",
574+
"/*\n"
575+
" */- (void)foo;");
576+
570577
Style.ColumnLimit = 60;
571578
verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
572579
" y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsObjCMethodExpr) {
18491849
EXPECT_TOKEN(Tokens[15], tok::greater, TT_BinaryOperator);
18501850
}
18511851

1852+
TEST_F(TokenAnnotatorTest, UnderstandsObjCMethodDecl) {
1853+
auto Tokens = annotate("/**/ - (void)foo;");
1854+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1855+
EXPECT_TOKEN(Tokens[1], tok::minus, TT_ObjCMethodSpecifier);
1856+
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_SelectorName);
1857+
}
1858+
18521859
TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
18531860
auto Tokens = annotate("[]() constexpr {}");
18541861
ASSERT_EQ(Tokens.size(), 8u) << Tokens;

0 commit comments

Comments
 (0)