[clang] eaef54f - [clang-format] Revert a feature in RemoveBracesLLVM
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 3 02:56:39 PST 2022
Author: Owen Pan
Date: 2022-02-03T02:56:09-08:00
New Revision: eaef54f21388350ca72d4dadf33728f70566e531
URL: https://github.com/llvm/llvm-project/commit/eaef54f21388350ca72d4dadf33728f70566e531
DIFF: https://github.com/llvm/llvm-project/commit/eaef54f21388350ca72d4dadf33728f70566e531.diff
LOG: [clang-format] Revert a feature in RemoveBracesLLVM
Revert the handling of a single-statement block that gets wrapped.
See issue #53543.
Differential Revision: https://reviews.llvm.org/D118873
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/lib/Format/UnwrappedLineParser.h
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index cdc2740ba9642..c43c8da6f3984 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -14,7 +14,6 @@
#include "UnwrappedLineParser.h"
#include "FormatToken.h"
-#include "TokenAnnotator.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
@@ -438,34 +437,8 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
(Previous->IsMultiline || Previous->NewlinesBefore > 0);
}
-bool UnwrappedLineParser::mightFitOnOneLine() const {
- const auto ColumnLimit = Style.ColumnLimit;
- if (ColumnLimit == 0)
- return true;
-
- if (Lines.empty())
- return true;
-
- const auto &PreviousLine = Lines.back();
- const auto &Tokens = PreviousLine.Tokens;
- assert(!Tokens.empty());
- const auto *LastToken = Tokens.back().Tok;
- assert(LastToken);
- if (!LastToken->isOneOf(tok::semi, tok::comment))
- return true;
-
- AnnotatedLine Line(PreviousLine);
- assert(Line.Last == LastToken);
-
- TokenAnnotator Annotator(Style, Keywords);
- Annotator.annotate(Line);
- Annotator.calculateFormattingInformation(Line);
-
- return Line.Level * Style.IndentWidth + LastToken->TotalLength <= ColumnLimit;
-}
-
// Returns true if a simple block, or false otherwise. (A simple block has a
-// single statement that fits on a single line.)
+// single statement.)
bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace, IfStmtKind *IfKind) {
const bool IsPrecededByCommentOrPPDirective =
!Style.RemoveBracesLLVM || precededByCommentOrPPDirective();
@@ -502,9 +475,7 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace, IfStmtKind *IfKind) {
precededByCommentOrPPDirective())
return false;
const FormatToken *Next = Tokens->peekNextToken();
- if (Next->is(tok::comment) && Next->NewlinesBefore == 0)
- return false;
- return mightFitOnOneLine();
+ return Next->isNot(tok::comment) || Next->NewlinesBefore > 0;
}
nextToken();
addUnwrappedLine();
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 3f64d57c7bff7..f39d76187f440 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -92,7 +92,6 @@ class UnwrappedLineParser {
void reset();
void parseFile();
bool precededByCommentOrPPDirective() const;
- bool mightFitOnOneLine() const;
bool parseLevel(bool HasOpeningBrace, IfStmtKind *IfKind = nullptr);
IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
bool MunchSemi = true,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 866847a531355..720c6e9b6b2f1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -23965,6 +23965,19 @@ TEST_F(FormatTest, RemoveBraces) {
"};",
Style);
+ // FIXME: See https://github.com/llvm/llvm-project/issues/53543.
+#if 0
+ Style.ColumnLimit = 65;
+
+ verifyFormat("if (condition) {\n"
+ " ff(Indices,\n"
+ " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
+ "} else {\n"
+ " ff(Indices,\n"
+ " [&](unsigned LHSI, unsigned RHSI) { return true; });\n"
+ "}",
+ Style);
+
Style.ColumnLimit = 20;
verifyFormat("if (a) {\n"
@@ -23981,6 +23994,9 @@ TEST_F(FormatTest, RemoveBraces) {
" b = c >= 0 ? d : e;\n"
"}",
Style);
+#endif
+
+ Style.ColumnLimit = 20;
verifyFormat("if (a)\n"
" b = c > 0 ? d : e;",
More information about the cfe-commits
mailing list