summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2012-12-24 00:13:23 +0000
committerDaniel Jasper <djasper@google.com>2012-12-24 00:13:23 +0000
commitde5c20792d3e684b3afac824f41076fca96f5813 (patch)
tree03f44e36c2dce22eace12d0a1e58a78fbde34fcd
parent32e0aa3a50d345d156402ded50adf783fd54d8c8 (diff)
downloadbcm5719-llvm-de5c20792d3e684b3afac824f41076fca96f5813.tar.gz
bcm5719-llvm-de5c20792d3e684b3afac824f41076fca96f5813.zip
Take operator precedence into account when splitting lines.
With this patch, splitting after binary operators has a panelty corresponding to the operator's precedence. We used to ignore this and eagerly format like: if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) { .. } With this patch, this becomes: if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) { .. } llvm-svn: 171007
-rw-r--r--clang/lib/Format/Format.cpp10
-rw-r--r--clang/unittests/Format/FormatTest.cpp15
2 files changed, 22 insertions, 3 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2a8fbd6e5e9..ad85a8ecf22 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -90,7 +90,7 @@ public:
: Style(Style), SourceMgr(SourceMgr), Line(Line),
Annotations(Annotations), Replaces(Replaces),
StructuralError(StructuralError) {
- Parameters.PenaltyIndentLevel = 5;
+ Parameters.PenaltyIndentLevel = 15;
}
void format() {
@@ -325,10 +325,14 @@ private:
if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma))
return 0;
- if (Left.Tok.is(tok::equal) || Left.Tok.is(tok::l_paren) ||
- Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::ampamp))
+ if (Left.Tok.is(tok::l_paren))
return 2;
+ prec::Level Level =
+ getBinOpPrecedence(Line.Tokens[Index].Tok.getKind(), true, true);
+ if (Level != prec::Unknown)
+ return Level;
+
if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
return 200;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index b316750a0b6..0d7901525b8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -456,6 +456,21 @@ TEST_F(FormatTest, BreaksDesireably) {
" (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
}
+TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
+ verifyFormat(
+ "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
+ verifyFormat(
+ "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
+ " ccccccccccccccccccccccccc) {\n}");
+ verifyFormat(
+ "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
+ " ccccccccccccccccccccccccc) {\n}");
+ verifyFormat(
+ "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
+ " ccccccccccccccccccccccccc) {\n}");
+}
+
TEST_F(FormatTest, AlignsStringLiterals) {
verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
" \"short literal\");");
OpenPOWER on IntegriCloud