-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-format] Align trailing comments for function parameters #164458
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-format Author: None (sstwcw) Changesbefore void foo(int name, // name
float name, // name
int name) // name
{}after void foo(int name, // name
float name, // name
int name) // name
{}Fixes #85123. As the bug report explained, the procedure for aligning the function parameters previously failed to update Full diff: https://github.com/llvm/llvm-project/pull/164458.diff 2 Files Affected:
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 65fc65e79fdc3..b9a261c9e91d2 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -399,6 +399,13 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
}
}
}
+
+ // The scope to be aligned may not occupy entire lines. The rest of the line
+ // needs some book-keeping.
+ for (unsigned i = End; i < Changes.size() && Changes[i].NewlinesBefore == 0;
+ ++i) {
+ Changes[i].StartOfTokenColumn += Shift;
+ }
}
// Walk through a subset of the changes, starting at StartAt, and find
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ce68f91bef02a..9133eeb137cb9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -19828,6 +19828,14 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
" Test &operator=(const Test &) = default;\n"
"};",
Alignment);
+
+ // The comment to the right should still align right.
+ verifyFormat("void foo(int name, // name\n"
+ " float name, // name\n"
+ " int name) // name\n"
+ "{}",
+ Alignment);
+
unsigned OldColumnLimit = Alignment.ColumnLimit;
// We need to set ColumnLimit to zero, in order to stress nested alignments,
// otherwise the function parameters will be re-flowed onto a single line.
|
before
```C++
void foo(int name, // name
float name, // name
int name) // name
{}
```
after
```C++
void foo(int name, // name
float name, // name
int name) // name
{}
```
Fixes llvm#85123.
As the bug report explained, the procedure for aligning the function
parameters previously failed to update `StartOfTokenColumn`.
aa2a818 to
1db4303
Compare
77b5fb5 to
7f148b2
Compare
HazardyKnusperkeks
approved these changes
Oct 22, 2025
owenca
reviewed
Oct 25, 2025
HazardyKnusperkeks
approved these changes
Oct 27, 2025
owenca
reviewed
Nov 3, 2025
df3649e to
3168df9
Compare
owenca
approved these changes
Nov 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
before
after
Fixes #85123.
As the bug report explained, the procedure for aligning the function parameters previously failed to update
StartOfTokenColumn.