Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -566,6 +566,40 @@ /* some comment */ #define bar(y, z) (y + z) +**AlignConsecutiveMacrosIgnoreMax** (``Boolean``) :versionbadge:`clang-format 15` + Macros beyond ``AlignConsecutiveMacrosMinWidth`` break the + ConsecutiveMacroAlignment streak. When used in conjuction + with ``AlignConsecutiveMacrosMinWidth`` this allows longer macros to + not cause shorter macros to align. + + + .. code-block:: c++ + + #define IDC_OK 1 + #define IDC_VERYVERYVERY_LONG 3 + #define IDC_CANCEL 2 + +**AlignConsecutiveMacrosMinWidth** (``Unsigned``) :versionbadge:`clang-format 15` + Set the Minimum macro alignment width to be used (default 0) + When used with ``AlignConsecutiveMacros`` + + Possible values: + + (default 0 or not specified) + + .. code-block:: c++ + + #define IDC_OK 1 + #define IDC_CANCEL 2 + + When ``AlignConsecutiveMacrosMinWidth`` is set to 40 + + + .. code-block:: c++ + + #define IDC_OK 1 + #define IDC_CANCEL 2 + **AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) :versionbadge:`clang-format 5` Options for aligning backslashes in escaped newlines. Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -223,6 +223,39 @@ /// \version 9 AlignConsecutiveStyle AlignConsecutiveMacros; + /// Macros beyond ``AlignConsecutiveMacrosMinWidth`` break the + /// ConsecutiveMacroAlignment streak. When used in conjuction + /// with ``AlignConsecutiveMacrosMinWidth`` this allows longer macros to + /// not cause shorter macros to align. + /// + /// \code + /// #define IDC_OK 1 + /// #define IDC_VERYVERYVERY_LONG 3 + /// #define IDC_CANCEL 2 + /// \endcode + /// \version 15 + bool AlignConsecutiveMacrosIgnoreMax; + + /// Set the Minimum macro alignment width to be used (default 0) + /// When used with ``AlignConsecutiveMacros`` + /// + /// Possible values: + /// + /// (default 0 or not specified) + /// \code + /// #define IDC_OK 1 + /// #define IDC_CANCEL 2 + /// \endcode + /// + /// When ``AlignConsecutiveMacrosMinWidth`` is set to 40 + /// + /// \code + /// #define IDC_OK 1 + /// #define IDC_CANCEL 2 + /// \endcode + /// \version 15 + unsigned AlignConsecutiveMacrosMinWidth; + /// Style of aligning consecutive assignments. /// /// ``Consecutive`` will result in formattings like: @@ -3983,6 +4016,9 @@ AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && AlignConsecutiveDeclarations == R.AlignConsecutiveDeclarations && AlignConsecutiveMacros == R.AlignConsecutiveMacros && + AlignConsecutiveMacrosIgnoreMax == + R.AlignConsecutiveMacrosIgnoreMax && + AlignConsecutiveMacrosMinWidth == R.AlignConsecutiveMacrosMinWidth && AlignEscapedNewlines == R.AlignEscapedNewlines && AlignOperands == R.AlignOperands && AlignTrailingComments == R.AlignTrailingComments && Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -601,6 +601,11 @@ IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures); IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros); + IO.mapOptional("AlignConsecutiveMacrosIgnoreMax", + Style.AlignConsecutiveMacrosIgnoreMax); + IO.mapOptional("AlignConsecutiveMacrosMinWidth", + Style.AlignConsecutiveMacrosMinWidth); + IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); IO.mapOptional("AlignConsecutiveBitFields", @@ -1145,6 +1150,8 @@ LLVMStyle.AlignConsecutiveBitFields = FormatStyle::ACS_None; LLVMStyle.AlignConsecutiveDeclarations = FormatStyle::ACS_None; LLVMStyle.AlignConsecutiveMacros = FormatStyle::ACS_None; + LLVMStyle.AlignConsecutiveMacrosIgnoreMax = false; + LLVMStyle.AlignConsecutiveMacrosMinWidth = 0; LLVMStyle.AllowAllArgumentsOnNextLine = true; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortEnumsOnASingleLine = true; Index: clang/lib/Format/WhitespaceManager.cpp =================================================================== --- clang/lib/Format/WhitespaceManager.cpp +++ clang/lib/Format/WhitespaceManager.cpp @@ -600,11 +600,15 @@ // // We need to adjust the StartOfTokenColumn of each Change that is on a line // containing any matching token to be aligned and located after such token. -static void AlignMacroSequence( - unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn, - unsigned &MaxColumn, bool &FoundMatchOnLine, - std::function AlignMacrosMatches, - SmallVector &Changes) { +static void +AlignMacroSequence(unsigned &StartOfSequence, unsigned &EndOfSequence, + unsigned &MinColumn, unsigned &MaxColumn, + bool &FoundMatchOnLine, + std::function + AlignMacrosMatches, + SmallVector &Changes, + const FormatStyle &Style) { if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) { FoundMatchOnLine = false; @@ -619,7 +623,7 @@ // If this is the first matching token to be aligned, remember by how many // spaces it has to be shifted, so the rest of the changes on the line are // shifted by the same amount - if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) { + if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I], Style)) { FoundMatchOnLine = true; Shift = MinColumn - Changes[I].StartOfTokenColumn; Changes[I].Spaces += Shift; @@ -632,7 +636,7 @@ } } - MinColumn = 0; + MinColumn = Style.AlignConsecutiveMacrosMinWidth; MaxColumn = UINT_MAX; StartOfSequence = 0; EndOfSequence = 0; @@ -642,7 +646,7 @@ if (Style.AlignConsecutiveMacros == FormatStyle::ACS_None) return; - auto AlignMacrosMatches = [](const Change &C) { + auto AlignMacrosMatches = [](const Change &C, const FormatStyle &Style) { const FormatToken *Current = C.Tok; unsigned SpacesRequiredBefore = 1; @@ -664,6 +668,10 @@ if (!Current->Previous || !Current->Previous->is(tok::pp_define)) return false; + if (Style.AlignConsecutiveMacrosIgnoreMax && + C.StartOfTokenColumn > Style.AlignConsecutiveMacrosMinWidth) + return false; + // For a macro function, 0 spaces are required between the // identifier and the lparen that opens the parameter list. // For a simple macro, 1 space is required between the @@ -671,7 +679,7 @@ return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore; }; - unsigned MinColumn = 0; + unsigned MinColumn = Style.AlignConsecutiveMacrosMinWidth; unsigned MaxColumn = UINT_MAX; // Start and end of the token sequence we're processing. @@ -707,7 +715,8 @@ if (EmptyLineBreak || NoMatchBreak) AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn, - FoundMatchOnLine, AlignMacrosMatches, Changes); + FoundMatchOnLine, AlignMacrosMatches, Changes, + Style); // A new line starts, re-initialize line status tracking bools. FoundMatchOnLine = false; @@ -717,7 +726,7 @@ if (!Changes[I].Tok->is(tok::comment)) LineIsComment = false; - if (!AlignMacrosMatches(Changes[I])) + if (!AlignMacrosMatches(Changes[I], Style)) continue; FoundMatchOnLine = true; @@ -737,7 +746,7 @@ EndOfSequence = I; AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn, - FoundMatchOnLine, AlignMacrosMatches, Changes); + FoundMatchOnLine, AlignMacrosMatches, Changes, Style); } void WhitespaceManager::alignConsecutiveAssignments() { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -16112,6 +16112,31 @@ Style)); } +TEST_F(FormatTest, AlignConsecutiveMacrosMinWidth) { + FormatStyle Style = getLLVMStyle(); + Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; + verifyFormat("#define a 3\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); + Style.AlignConsecutiveMacrosMinWidth = 30; + verifyFormat("#define a 3\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); + + verifyFormat("#define a 3\n" + "#define vvvvvvvvvverrrryyyyylonglonglongmacro 4\n" + "#define ccc (5)", + Style); + + Style.AlignConsecutiveMacrosIgnoreMax = true; + verifyFormat("#define a 3\n" + "#define vvvvvvvvvverrrryyyyylonglonglongmacro 4\n" + "#define ccc (5)", + Style); +} + TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { FormatStyle Alignment = getLLVMStyle(); Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; @@ -19575,6 +19600,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { FormatStyle Style = {}; Style.Language = FormatStyle::LK_Cpp; + CHECK_PARSE_BOOL(AlignConsecutiveMacrosIgnoreMax); CHECK_PARSE_BOOL(AlignTrailingComments); CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); @@ -19710,6 +19736,9 @@ CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLinesAndComments); + CHECK_PARSE("AlignConsecutiveMacrosMinWidth: 32", + AlignConsecutiveMacrosMinWidth, 32u); + // For backwards compability, false / true should still parse CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, FormatStyle::ACS_None);