diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -222,6 +222,8 @@ /// bbb = 2; /// \endcode bool AlignCompound; + // TODO + bool AcrossParameterDeclarations; /// Only for ``AlignConsecutiveAssignments``. Whether short assignment /// operators are left-padded to the same length as long ones in order to /// put all assignment operators to the right of the left hand side. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -12,7 +12,6 @@ /// //===----------------------------------------------------------------------===// -#include "clang/Format/Format.h" #include "AffectedRangeManager.h" #include "BreakableToken.h" #include "ContinuationIndenter.h" @@ -33,6 +32,7 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Lex/Lexer.h" #include "clang/Tooling/Inclusions/HeaderIncludes.h" #include "llvm/ADT/STLExtras.h" @@ -65,29 +65,44 @@ FormatStyle::AlignConsecutiveStyle( {/*Enabled=*/false, /*AcrossEmptyLines=*/false, /*AcrossComments=*/false, /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ true, /*PadOperators=*/true})); IO.enumCase(Value, "Consecutive", FormatStyle::AlignConsecutiveStyle( {/*Enabled=*/true, /*AcrossEmptyLines=*/false, /*AcrossComments=*/false, /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ true, /*PadOperators=*/true})); IO.enumCase(Value, "AcrossEmptyLines", FormatStyle::AlignConsecutiveStyle( {/*Enabled=*/true, /*AcrossEmptyLines=*/true, /*AcrossComments=*/false, /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ true, /*PadOperators=*/true})); IO.enumCase(Value, "AcrossComments", - FormatStyle::AlignConsecutiveStyle({/*Enabled=*/true, - /*AcrossEmptyLines=*/false, - /*AcrossComments=*/true, - /*AlignCompound=*/false, - /*PadOperators=*/true})); + FormatStyle::AlignConsecutiveStyle( + {/*Enabled=*/true, + /*AcrossEmptyLines=*/false, + /*AcrossComments=*/true, + /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ true, + /*PadOperators=*/true})); IO.enumCase(Value, "AcrossEmptyLinesAndComments", - FormatStyle::AlignConsecutiveStyle({/*Enabled=*/true, - /*AcrossEmptyLines=*/true, - /*AcrossComments=*/true, - /*AlignCompound=*/false, - /*PadOperators=*/true})); + FormatStyle::AlignConsecutiveStyle( + {/*Enabled=*/true, + /*AcrossEmptyLines=*/true, + /*AcrossComments=*/true, + /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ true, + /*PadOperators=*/true})); + IO.enumCase(Value, "ConsecutiveMinusParameterDeclarations", + FormatStyle::AlignConsecutiveStyle( + {/*Enabled=*/true, + /*AcrossEmptyLines=*/false, + /*AcrossComments=*/false, + /*AlignCompound=*/false, + /*AcrossParameterDeclarations*/ false, + /*PadOperators=*/true})); // For backward compatibility. IO.enumCase(Value, "true", diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -870,7 +870,7 @@ AlignTokens( Style, - [](Change const &C) { + [&](Change const &C) { if (C.Tok->is(TT_FunctionDeclarationName)) return true; if (C.Tok->isNot(TT_StartOfName)) @@ -878,6 +878,15 @@ if (C.Tok->Previous && C.Tok->Previous->is(TT_StatementAttributeLikeMacro)) return false; + + if (C.Tok->is(TT_StartOfName)) { + bool isParam = C.Tok->Next && C.Tok->Next->is(clang::tok::comma); + bool shouldAlignParams = + Style.AlignConsecutiveDeclarations.AcrossParameterDeclarations; + if (isParam && !shouldAlignParams) + return false; + } + // Check if there is a subsequent name that starts the same declaration. for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) { if (Next->is(tok::comment))