Skip to content

Commit fa09d03

Browse files
authored
[clang-format] Correctly annotate ObjC * __autoreleasing * (#138799)
Fix #138484
1 parent ab2e7aa commit fa09d03

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@ struct FormatToken {
711711
tok::objc_package, tok::objc_private);
712712
}
713713

714+
bool isObjCLifetimeQualifier(const FormatStyle &Style) const {
715+
if (Style.Language != FormatStyle::LK_ObjC || !TokenText.starts_with("__"))
716+
return false;
717+
const auto Qualifier = TokenText.substr(2);
718+
return Qualifier == "autoreleasing" || Qualifier == "strong" ||
719+
Qualifier == "weak" || Qualifier == "unsafe_unretained";
720+
}
721+
714722
/// Returns whether \p Tok is ([{ or an opening < of a template or in
715723
/// protos.
716724
bool opensScope() const {

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3100,7 +3100,8 @@ class AnnotatingParser {
31003100
if (NextNextToken) {
31013101
if (NextNextToken->is(tok::arrow))
31023102
return TT_BinaryOperator;
3103-
if (NextNextToken->isPointerOrReference()) {
3103+
if (NextNextToken->isPointerOrReference() &&
3104+
!NextToken->isObjCLifetimeQualifier(Style)) {
31043105
NextNextToken->setFinalizedType(TT_BinaryOperator);
31053106
return TT_BinaryOperator;
31063107
}

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
411411
ASSERT_EQ(Tokens.size(), 27u) << Tokens;
412412
EXPECT_TOKEN(Tokens[16], tok::star, TT_BinaryOperator);
413413
EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
414+
415+
Tokens = annotate("NSError *__autoreleasing *foo;",
416+
getLLVMStyle(FormatStyle::LK_ObjC));
417+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
418+
EXPECT_TOKEN(Tokens[1], tok::star, TT_PointerOrReference);
419+
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
414420
}
415421

416422
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {

0 commit comments

Comments
 (0)