Skip to content

Commit

Permalink
[clang-format] Fix PointerAlignment: Right not working with tab inden…
Browse files Browse the repository at this point in the history
…tation.

Fixes #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
  • Loading branch information
mkurdej committed May 16, 2022
1 parent 7ff0bf5 commit e20bc89
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/Format/WhitespaceManager.cpp
Expand Up @@ -432,6 +432,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
--Previous) {
Changes[Previous + 1].Spaces -= Shift;
Changes[Previous].Spaces += Shift;
Changes[Previous].StartOfTokenColumn += Shift;
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -14161,6 +14161,21 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"int bbbbbbbb; // x\n",
Tab);

FormatStyle TabAlignment = Tab;
TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
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"
Expand Down Expand Up @@ -14203,6 +14218,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"
Expand Down

0 comments on commit e20bc89

Please sign in to comment.