Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion clang/lib/Format/QualifierAlignmentFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(Prev && Prev->is(tok::identifier)))
if (!Prev || Prev->isNot(tok::identifier))

I personally don't like negated parenthesis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither do I when the conditions inside the parentheses are unrelated. Otherwise, I prefer the negated parentheses. Another example is !(Foo == A || Foo == B || Foo == C) (i.e. Foo is not one of A, B, and C) vs Foo != A && Foo != B && Foo != C (i.e. Foo is not A, not B, and not C). The former is more readable to me.

break;
TypeToken = Prev;
}
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
while (isConfiguredQualifierOrType(
LastSimpleTypeSpecifier->getPreviousNonComment(),
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/QualifierFixerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down