[clang-format] Hanlde qualified type name for QualifierAlignment (#125327)

Fixes #125178.
This commit is contained in:
Owen Pan 2025-02-04 01:33:44 -08:00 committed by GitHub
parent cdca04913a
commit eb6ca1242c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -132,8 +132,10 @@ static void rotateTokens(const SourceManager &SourceMgr,
// Then move through the other tokens.
auto *Tok = Begin;
while (Tok != End) {
if (!NewText.empty() && !endsWithSpace(NewText))
if (!NewText.empty() && !endsWithSpace(NewText) &&
Tok->isNot(tok::coloncolon)) {
NewText += " ";
}
NewText += Tok->TokenText;
Tok = Tok->Next;
@ -412,6 +414,14 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
// The case `const long long volatile int` -> `const volatile long long int`
// The case `long volatile long int const` -> `const volatile long long int`
if (TypeToken->isTypeName(LangOpts)) {
for (const auto *Prev = TypeToken->Previous;
Prev && Prev->is(tok::coloncolon); Prev = Prev->Previous) {
TypeToken = Prev;
Prev = Prev->Previous;
if (!(Prev && Prev->is(tok::identifier)))
break;
TypeToken = Prev;
}
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
while (isConfiguredQualifierOrType(
LastSimpleTypeSpecifier->getPreviousNonComment(),

View File

@ -1291,6 +1291,21 @@ TEST_F(QualifierFixerTest, WithCpp11Attribute) {
"[[maybe_unused]] constexpr static int A", Style);
}
TEST_F(QualifierFixerTest, WithQualifiedTypeName) {
auto Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"constexpr", "type", "const"};
verifyFormat("constexpr ::int64_t x{1};", "::int64_t constexpr x{1};", Style);
verifyFormat("constexpr std::int64_t x{123};",
"std::int64_t constexpr x{123};", Style);
verifyFormat("constexpr ::std::int64_t x{123};",
"::std::int64_t constexpr x{123};", Style);
Style.TypeNames.push_back("bar");
verifyFormat("constexpr foo::bar x{12};", "foo::bar constexpr x{12};", Style);
}
TEST_F(QualifierFixerTest, DisableRegions) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;