Skip to content

Commit 8215069

Browse files
owencatstellar
authored andcommitted
[clang-format] Hanlde qualified type name for QualifierAlignment (#125327)
Fixes #125178. (cherry picked from commit eb6ca12)
1 parent 539174b commit 8215069

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

clang/lib/Format/QualifierAlignmentFixer.cpp

Lines changed: 11 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,14 @@ 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+
for (const auto *Prev = TypeToken->Previous;
418+
Prev && Prev->is(tok::coloncolon); Prev = Prev->Previous) {
419+
TypeToken = Prev;
420+
Prev = Prev->Previous;
421+
if (!(Prev && Prev->is(tok::identifier)))
422+
break;
423+
TypeToken = Prev;
424+
}
415425
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
416426
while (isConfiguredQualifierOrType(
417427
LastSimpleTypeSpecifier->getPreviousNonComment(),

clang/unittests/Format/QualifierFixerTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,21 @@ 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 ::int64_t x{1};", "::int64_t constexpr x{1};", Style);
1300+
verifyFormat("constexpr std::int64_t x{123};",
1301+
"std::int64_t constexpr x{123};", Style);
1302+
verifyFormat("constexpr ::std::int64_t x{123};",
1303+
"::std::int64_t constexpr x{123};", Style);
1304+
1305+
Style.TypeNames.push_back("bar");
1306+
verifyFormat("constexpr foo::bar x{12};", "foo::bar constexpr x{12};", Style);
1307+
}
1308+
12941309
TEST_F(QualifierFixerTest, DisableRegions) {
12951310
FormatStyle Style = getLLVMStyle();
12961311
Style.QualifierAlignment = FormatStyle::QAS_Custom;

0 commit comments

Comments
 (0)