diff options
author | Marek Kurdej <marek.kurdej+llvm.org@gmail.com> | 2022-05-13 11:27:35 +0200 |
---|---|---|
committer | Tom Stellard <tstellar@redhat.com> | 2022-06-09 13:04:34 -0700 |
commit | 3cd9df8443f8d5bd308e2417a77bebeef5264327 (patch) | |
tree | 47d59e632d08506a050095ffeb11617110ff9f19 | |
parent | d350783a0520d09eb61e2eca1cd61c9cdac00908 (diff) | |
download | llvm-3cd9df8443f8d5bd308e2417a77bebeef5264327.tar.gz |
[clang-format] Fix PointerAlignment: Right not working with tab indentation.
Fixes https://github.com/llvm/llvm-project/issues/55407.
Given configuration:
```
UseTab: Always
PointerAlignment: Right
AlignConsecutiveDeclarations: true
```
Before, the pointer was misaligned in this code:
```
void f() {
unsigned long long big;
char *ptr; // misaligned
int i;
}
```
That was due to the fact that when handling right-aligned pointers, the Spaces were changed but StartOfTokenColumn was not.
Also, a tab was used not only for indentation but for spacing too when using `UseTab: ForIndentation` config option:
```
void f() {
unsigned long long big;
char *ptr; // \t after char
int i;
}
```
Reviewed By: owenpan
Differential Revision: https://reviews.llvm.org/D125528
-rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 1 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 4c130abd83c3..f5a0b9963b5d 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -414,6 +414,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, --Previous) { Changes[Previous + 1].Spaces -= Shift; Changes[Previous].Spaces += Shift; + Changes[Previous].StartOfTokenColumn += Shift; } } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 136a25fa8946..b621fe7e06f1 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -13687,6 +13687,21 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "int bbbbbbbb; // x\n", Tab); + FormatStyle TabAlignment = Tab; + TabAlignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; + TabAlignment.PointerAlignment = FormatStyle::PAS_Left; + verifyFormat("unsigned long long big;\n" + "char*\t\t ptr;", + TabAlignment); + TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; + verifyFormat("unsigned long long big;\n" + "char *\t\t ptr;", + TabAlignment); + TabAlignment.PointerAlignment = FormatStyle::PAS_Right; + verifyFormat("unsigned long long big;\n" + "char\t\t *ptr;", + TabAlignment); + Tab.TabWidth = 4; Tab.IndentWidth = 8; verifyFormat("class TabWidth4Indent8 {\n" @@ -13729,6 +13744,26 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { " \t */", Tab)); + TabAlignment.UseTab = FormatStyle::UT_ForIndentation; + TabAlignment.PointerAlignment = FormatStyle::PAS_Left; + verifyFormat("void f() {\n" + "\tunsigned long long big;\n" + "\tchar* ptr;\n" + "}", + TabAlignment); + TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; + verifyFormat("void f() {\n" + "\tunsigned long long big;\n" + "\tchar * ptr;\n" + "}", + TabAlignment); + TabAlignment.PointerAlignment = FormatStyle::PAS_Right; + verifyFormat("void f() {\n" + "\tunsigned long long big;\n" + "\tchar *ptr;\n" + "}", + TabAlignment); + Tab.UseTab = FormatStyle::UT_ForIndentation; verifyFormat("{\n" "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |