From: Daniel Jasper Date: Tue, 20 Dec 2016 15:27:46 +0000 (+0000) Subject: clang-format: Fix bug in understanding string-label&value analysis. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4aeb870bf4b2e0ea0cd0a926b4ed741e72e606ef;p=clang clang-format: Fix bug in understanding string-label&value analysis. 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 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 5dcaf2651f..7f57306159 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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)) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index af78419706..27c946e6ff 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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) {