Skip to content

Commit

Permalink
Revert "Revert "[clang-format] Fix overlapping replacements before PP…
Browse files Browse the repository at this point in the history
…Directives""

This reverts commit 94e7546.

Apparently I broke some builders with the original revert: http://45.33.8.238/linux/109159/step_7.txt
  • Loading branch information
PiJoules committed Jun 8, 2023
1 parent b7f5f48 commit 9f00eb9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
42 changes: 29 additions & 13 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,19 +1418,12 @@ unsigned UnwrappedLineFormatter::format(
return Penalty;
}

void UnwrappedLineFormatter::formatFirstToken(
const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
const AnnotatedLine *PrevPrevLine,
const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
unsigned NewlineIndent) {
FormatToken &RootToken = *Line.First;
if (RootToken.is(tok::eof)) {
unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
unsigned TokenIndent = Newlines ? NewlineIndent : 0;
Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
TokenIndent);
return;
}
static auto newlinesBeforeLine(const AnnotatedLine &Line,
const AnnotatedLine *PreviousLine,
const AnnotatedLine *PrevPrevLine,
const SmallVectorImpl<AnnotatedLine *> &Lines,
const FormatStyle &Style) {
const auto &RootToken = *Line.First;
unsigned Newlines =
std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
// Remove empty lines before "}" where applicable.
Expand Down Expand Up @@ -1510,6 +1503,29 @@ void UnwrappedLineFormatter::formatFirstToken(
}
}

return Newlines;
}

void UnwrappedLineFormatter::formatFirstToken(
const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
const AnnotatedLine *PrevPrevLine,
const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
unsigned NewlineIndent) {
FormatToken &RootToken = *Line.First;
if (RootToken.is(tok::eof)) {
unsigned Newlines =
std::min(RootToken.NewlinesBefore,
Style.KeepEmptyLinesAtEOF ? Style.MaxEmptyLinesToKeep + 1 : 1);
unsigned TokenIndent = Newlines ? NewlineIndent : 0;
Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
TokenIndent);
return;
}

const auto Newlines =
RootToken.Finalized
? RootToken.NewlinesBefore
: newlinesBeforeLine(Line, PreviousLine, PrevPrevLine, Lines, Style);
if (Newlines)
Indent = NewlineIndent;

Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12856,6 +12856,22 @@ TEST_F(FormatTest, FormatsAfterAccessModifiers) {
" void f() {}\n"
"};\n",
Style);
verifyFormat("struct foo {\n"
"#ifdef FOO\n"
"#else\n"
"private:\n"
"\n"
"#endif\n"
"};",
"struct foo {\n"
"#ifdef FOO\n"
"#else\n"
"private:\n"
"\n"
"\n"
"#endif\n"
"};",
Style);

Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
verifyFormat("struct foo {\n"
Expand Down

0 comments on commit 9f00eb9

Please sign in to comment.