[clang] 4b97649 - [clang-format] Fix overlapping replacements before PPDirectives
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 3 12:33:29 PDT 2023
Author: Owen Pan
Date: 2023-06-03T12:33:03-07:00
New Revision: 4b9764959dc4b8783e18747c1742ab164e4bc4ee
URL: https://github.com/llvm/llvm-project/commit/4b9764959dc4b8783e18747c1742ab164e4bc4ee
DIFF: https://github.com/llvm/llvm-project/commit/4b9764959dc4b8783e18747c1742ab164e4bc4ee.diff
LOG: [clang-format] Fix overlapping replacements before PPDirectives
If the first token of an annotated line is finalized, reuse its
NewlinesBefore value to avoid potential overlapping whitespace
replacements before preprocessor branching directives.
Fixes #62892.
Differential Revision: https://reviews.llvm.org/D151954
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 33be74dfe1b9f..fc5d4150ed777 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -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.
@@ -1510,6 +1503,27 @@ 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, 1u);
+ 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;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 28a4008080566..f188ab6f581cf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
More information about the cfe-commits
mailing list