Skip to content

Commit 01b288f

Browse files
authored
[clang-format] Improve QualifierAlignment in guessing macros (#145468)
Fixes #145388
1 parent 34bfa4e commit 01b288f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,26 @@ bool isConfiguredQualifierOrType(const FormatToken *Tok,
635635
// If a token is an identifier and it's upper case, it could
636636
// be a macro and hence we need to be able to ignore it.
637637
bool isPossibleMacro(const FormatToken *Tok) {
638-
if (!Tok)
639-
return false;
638+
assert(Tok);
640639
if (Tok->isNot(tok::identifier))
641640
return false;
642-
if (Tok->TokenText.upper() == Tok->TokenText.str()) {
643-
// T,K,U,V likely could be template arguments
644-
return Tok->TokenText.size() != 1;
645-
}
646-
return false;
641+
642+
const auto Text = Tok->TokenText;
643+
assert(Text.size() > 0);
644+
645+
// T,K,U,V likely could be template arguments
646+
if (Text.size() == 1)
647+
return false;
648+
649+
// It's unlikely that qualified names are object-like macros.
650+
const auto *Prev = Tok->getPreviousNonComment();
651+
if (Prev && Prev->is(tok::coloncolon))
652+
return false;
653+
const auto *Next = Tok->getNextNonComment();
654+
if (Next && Next->is(tok::coloncolon))
655+
return false;
656+
657+
return Text == Text.upper();
647658
}
648659

649660
} // namespace format

clang/unittests/Format/QualifierFixerTest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,14 +1122,17 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
11221122
}
11231123

11241124
TEST_F(QualifierFixerTest, IsMacro) {
1125-
11261125
auto Tokens = annotate("INT INTPR Foo int");
11271126
ASSERT_EQ(Tokens.size(), 5u) << Tokens;
1128-
11291127
EXPECT_TRUE(isPossibleMacro(Tokens[0]));
11301128
EXPECT_TRUE(isPossibleMacro(Tokens[1]));
11311129
EXPECT_FALSE(isPossibleMacro(Tokens[2]));
11321130
EXPECT_FALSE(isPossibleMacro(Tokens[3]));
1131+
1132+
Tokens = annotate("FOO::BAR");
1133+
ASSERT_EQ(Tokens.size(), 4u) << Tokens;
1134+
EXPECT_FALSE(isPossibleMacro(Tokens[0]));
1135+
EXPECT_FALSE(isPossibleMacro(Tokens[2]));
11331136
}
11341137

11351138
TEST_F(QualifierFixerTest, OverlappingQualifier) {

0 commit comments

Comments
 (0)