Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,16 @@ the configuration (without a prefix: ``Auto``).

int abcdef; // but this isn't

* ``bool AlignPPAndNotPP`` If comments following preprocessor directive should be aligned with
comments that don't.

.. code-block:: c++

true: false:
#define A // Comment vs. #define A // Comment
#define AB // Aligned #define AB // Aligned
int i; // Aligned int i; // Not aligned


.. _AllowAllArgumentsOnNextLine:

Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ clang-format
literals.
- Add ``Leave`` suboption to ``IndentPPDirectives``.
- Add ``AllowBreakBeforeQtProperty`` option.
- Add ``AlignPPAndNotPP`` suboption to ``AlignTrailingComments``.

libclang
--------
Expand Down
12 changes: 11 additions & 1 deletion clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,19 @@ struct FormatStyle {
/// int abcdef; // but this isn't
/// \endcode
unsigned OverEmptyLines;
/// If comments following preprocessor directive should be aligned with
/// comments that don't.
/// \code
/// true: false:
/// #define A // Comment vs. #define A // Comment
/// #define AB // Aligned #define AB // Aligned
/// int i; // Aligned int i; // Not aligned
/// \endcode
bool AlignPPAndNotPP;

bool operator==(const TrailingCommentsAlignmentStyle &R) const {
return Kind == R.Kind && OverEmptyLines == R.OverEmptyLines;
return Kind == R.Kind && OverEmptyLines == R.OverEmptyLines &&
AlignPPAndNotPP == R.AlignPPAndNotPP;
}
bool operator!=(const TrailingCommentsAlignmentStyle &R) const {
return !(*this == R);
Expand Down
12 changes: 7 additions & 5 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,27 +869,28 @@ template <> struct MappingTraits<FormatStyle::TrailingCommentsAlignmentStyle> {
FormatStyle::TrailingCommentsAlignmentStyle &Value) {
IO.enumCase(Value, "Leave",
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Leave, 0}));
{FormatStyle::TCAS_Leave, 0, true}));

IO.enumCase(Value, "Always",
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Always, 0}));
{FormatStyle::TCAS_Always, 0, true}));

IO.enumCase(Value, "Never",
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Never, 0}));
{FormatStyle::TCAS_Never, 0, true}));

// For backwards compatibility
IO.enumCase(Value, "true",
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Always, 0}));
{FormatStyle::TCAS_Always, 0, true}));
IO.enumCase(Value, "false",
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Never, 0}));
{FormatStyle::TCAS_Never, 0, true}));
}

static void mapping(IO &IO,
FormatStyle::TrailingCommentsAlignmentStyle &Value) {
IO.mapOptional("AlignPPAndNotPP", Value.AlignPPAndNotPP);
IO.mapOptional("Kind", Value.Kind);
IO.mapOptional("OverEmptyLines", Value.OverEmptyLines);
}
Expand Down Expand Up @@ -1578,6 +1579,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignTrailingComments = {};
LLVMStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
LLVMStyle.AlignTrailingComments.OverEmptyLines = 0;
LLVMStyle.AlignTrailingComments.AlignPPAndNotPP = true;
LLVMStyle.AllowAllArgumentsOnNextLine = true;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
LLVMStyle.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Never;
Expand Down
18 changes: 17 additions & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,13 @@ void WhitespaceManager::alignTrailingComments() {
return;

const int Size = Changes.size();
if (Size == 0)
return;

int MinColumn = 0;
int StartOfSequence = 0;
bool BreakBeforeNext = false;
bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
int NewLineThreshold = 1;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
Expand All @@ -1008,7 +1012,19 @@ void WhitespaceManager::alignTrailingComments() {
auto &C = Changes[I];
if (C.StartOfBlockComment)
continue;
Newlines += C.NewlinesBefore;
if (C.NewlinesBefore != 0) {
Newlines += C.NewlinesBefore;
const bool WasInPP = std::exchange(
IsInPP, C.Tok->Tok.is(tok::hash) || (IsInPP && C.IsTrailingComment) ||
C.ContinuesPPDirective);
if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
alignTrailingComments(StartOfSequence, I, MinColumn);
MinColumn = 0;
MaxColumn = INT_MAX;
StartOfSequence = I;
Newlines = 0;
}
}
if (!C.IsTrailingComment)
continue;

Expand Down
11 changes: 6 additions & 5 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,20 +576,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {

CHECK_PARSE("AlignTrailingComments: Leave", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Leave, 0}));
{FormatStyle::TCAS_Leave, 0, true}));
CHECK_PARSE("AlignTrailingComments: Always", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Always, 0}));
{FormatStyle::TCAS_Always, 0, true}));
CHECK_PARSE("AlignTrailingComments: Never", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Never, 0}));
{FormatStyle::TCAS_Never, 0, true}));
// For backwards compatibility
CHECK_PARSE("AlignTrailingComments: true", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Always, 0}));
{FormatStyle::TCAS_Always, 0, true}));
CHECK_PARSE("AlignTrailingComments: false", AlignTrailingComments,
FormatStyle::TrailingCommentsAlignmentStyle(
{FormatStyle::TCAS_Never, 0}));
{FormatStyle::TCAS_Never, 0, true}));
CHECK_PARSE_NESTED_VALUE("Kind: Always", AlignTrailingComments, Kind,
FormatStyle::TCAS_Always);
CHECK_PARSE_NESTED_VALUE("Kind: Never", AlignTrailingComments, Kind,
Expand All @@ -598,6 +598,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::TCAS_Leave);
CHECK_PARSE_NESTED_VALUE("OverEmptyLines: 1234", AlignTrailingComments,
OverEmptyLines, 1234u);
CHECK_PARSE_NESTED_BOOL(AlignTrailingComments, AlignPPAndNotPP);

Style.UseTab = FormatStyle::UT_ForIndentation;
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
Expand Down
60 changes: 60 additions & 0 deletions clang/unittests/Format/FormatTestComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3501,6 +3501,66 @@ TEST_F(FormatTestComments, DontAlignOverScope) {
"int foobar; // group");
}

TEST_F(FormatTestComments, DontAlignOverPPDirective) {
auto Style = getLLVMStyle();
Style.AlignTrailingComments.AlignPPAndNotPP = false;

verifyFormat("int i; // Aligned\n"
"int long; // with this\n"
"#define FOO // only aligned\n"
"#define LOOONG // with other pp directives\n"
"int loooong; // new alignment",
"int i;//Aligned\n"
"int long;//with this\n"
"#define FOO //only aligned\n"
"#define LOOONG //with other pp directives\n"
"int loooong; //new alignment",
Style);

verifyFormat("#define A // Comment\n"
"#define AB // Comment",
Style);

Style.ColumnLimit = 30;
verifyNoChange("#define A // Comment\n"
" // Continued\n"
"int i = 0; // New Stuff\n"
" // Continued\n"
"#define Func(X) \\\n"
" X(); \\\n"
" X(); // Comment\n"
" // Continued\n"
"long loong = 1; // Dont align",
Style);

verifyFormat("#define A // Comment that\n"
" // would wrap\n"
"#define FOO // For the\n"
" // alignment\n"
"#define B // Also\n"
" // aligned",
"#define A // Comment that would wrap\n"
"#define FOO // For the alignment\n"
"#define B // Also\n"
" // aligned",
Style);

Style.AlignTrailingComments.OverEmptyLines = 1;
verifyNoChange("#define A // Comment\n"
"\n"
" // Continued\n"
"int i = 0; // New Stuff\n"
"\n"
" // Continued\n"
"#define Func(X) \\\n"
" X(); \\\n"
" X(); // Comment\n"
"\n"
" // Continued\n"
"long loong = 1; // Dont align",
Style);
}

TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
EXPECT_EQ("/*\n"
" */",
Expand Down