Skip to content

Commit af3d964

Browse files
committed
[clang-format] Hanlde qualified type names
Fixes #125178.
1 parent b873479 commit af3d964

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ static void rotateTokens(const SourceManager &SourceMgr,
132132
// Then move through the other tokens.
133133
auto *Tok = Begin;
134134
while (Tok != End) {
135-
if (!NewText.empty() && !endsWithSpace(NewText))
135+
if (!NewText.empty() && !endsWithSpace(NewText) &&
136+
Tok->isNot(tok::coloncolon)) {
136137
NewText += " ";
138+
}
137139

138140
NewText += Tok->TokenText;
139141
Tok = Tok->Next;
@@ -412,6 +414,10 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
412414
// The case `const long long volatile int` -> `const volatile long long int`
413415
// The case `long volatile long int const` -> `const volatile long long int`
414416
if (TypeToken->isTypeName(LangOpts)) {
417+
if (const auto *Prev = TypeToken->getPreviousNonComment();
418+
Prev && Prev->endsSequence(tok::coloncolon, tok::identifier)) {
419+
TypeToken = Prev->getPreviousNonComment();
420+
}
415421
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
416422
while (isConfiguredQualifierOrType(
417423
LastSimpleTypeSpecifier->getPreviousNonComment(),

clang/unittests/Format/QualifierFixerTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,18 @@ TEST_F(QualifierFixerTest, WithCpp11Attribute) {
12911291
"[[maybe_unused]] constexpr static int A", Style);
12921292
}
12931293

1294+
TEST_F(QualifierFixerTest, WithQualifiedTypeName) {
1295+
auto Style = getLLVMStyle();
1296+
Style.QualifierAlignment = FormatStyle::QAS_Custom;
1297+
Style.QualifierOrder = {"constexpr", "type", "const"};
1298+
1299+
verifyFormat("constexpr std::int64_t x{123};",
1300+
"std::int64_t constexpr x{123};", Style);
1301+
1302+
Style.TypeNames.push_back("bar");
1303+
verifyFormat("constexpr foo::bar x{12};", "foo::bar constexpr x{12};", Style);
1304+
}
1305+
12941306
TEST_F(QualifierFixerTest, DisableRegions) {
12951307
FormatStyle Style = getLLVMStyle();
12961308
Style.QualifierAlignment = FormatStyle::QAS_Custom;

0 commit comments

Comments
 (0)