-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #125178. Full diff: https://github.com/llvm/llvm-project/pull/125327.diff 2 Files Affected:
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 21fb5074b4928f..f777ac2a89f755 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -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,10 @@ 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)) {
+ if (const auto *Prev = TypeToken->getPreviousNonComment();
+ Prev && Prev->endsSequence(tok::coloncolon, tok::identifier)) {
+ TypeToken = Prev->getPreviousNonComment();
+ }
const FormatToken *LastSimpleTypeSpecifier = TypeToken;
while (isConfiguredQualifierOrType(
LastSimpleTypeSpecifier->getPreviousNonComment(),
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index 129828b0d187a9..530587bbf2ca74 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -1291,6 +1291,18 @@ 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 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;
|
QualifierAlignment
Seems to also work for top-level types ( |
Yeah, I intentionally didn't want to use a loop for names like |
But why? |
Same reason we almost never handle them, just like we (almost?) never handle the possibility of templated qualified names of arbitrary nesting levels (e.g. |
It didn't work either. |
Now it handles the general pattern above (and also |
Prev = Prev->getPreviousNonComment()) { | ||
TypeToken = Prev; | ||
Prev = Prev->getPreviousNonComment(); | ||
if (!(Prev && Prev->is(tok::identifier))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!(Prev && Prev->is(tok::identifier))) | |
if (!Prev || Prev->isNot(tok::identifier)) |
I personally don't like negated parenthesis.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you
✅ With the latest revision this PR passed the C/C++ code formatter. |
/cherry-pick eb6ca12 |
/pull-request #126839 |
…lvm#125327) Fixes llvm#125178. (cherry picked from commit eb6ca12)
Fixes #125178.