Author: Marek Kurdej Date: 2022-05-16T09:42:20+02:00 New Revision: e20bc892b6facc56fffc012929157888bb798bed
URL: https://github.com/llvm/llvm-project/commit/e20bc892b6facc56fffc012929157888bb798bed DIFF: https://github.com/llvm/llvm-project/commit/e20bc892b6facc56fffc012929157888bb798bed.diff LOG: [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 Added: Modified: clang/lib/Format/WhitespaceManager.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 8563312b45bb0..673af51d2daae 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -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; } } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e4fea9085b574..44da34001f990 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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" @@ -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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits