]> granicus.if.org Git - clang/commitdiff
clang-format: Fix bug in understanding string-label&value analysis.
authorDaniel Jasper <djasper@google.com>
Tue, 20 Dec 2016 15:27:46 +0000 (15:27 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 20 Dec 2016 15:27:46 +0000 (15:27 +0000)
While for <<-operators often used in log statments, a single key value
pair is always on the second operator, e.g.

  llvm::errs() << "aaaaa=" << aaaaa;

It is on the first operator for plus- or comma-concatenated strings:

  string s = "aaaaaaaaaa: " + aaaaaaaa;

(the "=" not counting because that's a different operator precedence)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@290177 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 5dcaf2651ff8e03b3b72d157f4732db8681d4595..7f5730615944900ed4ffb6c29b9a5122c50c50e0 100644 (file)
@@ -2000,11 +2000,14 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
 
   if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
       Left.Previous->isLabelString() &&
-      (Left.NextOperator || Left.OperatorIndex != 1))
+      (Left.NextOperator || Left.OperatorIndex != 0))
     return 100;
+  if (Right.is(tok::plus) && Left.isLabelString() &&
+      (Right.NextOperator || Right.OperatorIndex != 0))
+    return 25;
   if (Left.is(tok::comma))
     return 1;
-  if (Right.isOneOf(tok::lessless, tok::plus) && Left.isLabelString() &&
+  if (Right.is(tok::lessless) && Left.isLabelString() &&
       (Right.NextOperator || Right.OperatorIndex != 1))
     return 25;
   if (Right.is(tok::lessless)) {
index af784197062d3f302f52ef6d0aebe7235ae3d5c9..27c946e6ff02ac74f77e4c3999495178a0094ef0 100644 (file)
@@ -5214,6 +5214,12 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
+  verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
+               "           (aaaa + aaaa);",
+               getLLVMStyleWithColumns(40));
+  verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
+               "                  (aaaaaaa + aaaaa));",
+               getLLVMStyleWithColumns(40));
 }
 
 TEST_F(FormatTest, UnderstandsEquals) {