[PATCH] D80950: [clang-format] [PR44542,38872] String << String always get a forced newline.

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 1 14:05:41 PDT 2020


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: krasimir, sammccall, klimek, curdeius, JakeMerdichAMD, Abpostelnicu.
MyDeveloperDay added projects: clang, clang-format.
Herald added a reviewer: bollu.

https://bugs.llvm.org/show_bug.cgi?id=44542
https://bugs.llvm.org/show_bug.cgi?id=38872
(I'm sure I've seen others)

Users really don't like clang-format going off on one and breaking lines without their control, this piece of code which I'm removing dates all the way back to 2013, I think it was added because it people think

  os << "from"
     << "asdf"
     << "def"
     << "ghi"
     << "ghi";

is more pleasing.. however, when you start using other types, then this just becomes crazy... and leaves the user wondering why. (see bugs for other odd cases)

  os << "from"
     << "def" << 1 << "ghi"
     << "lmn"
     << "opq" << endl
     << "abc"
     << "ghi";

The rules I'm removing was to FORCE (MustBreak) a line break between `"<String>" << "<String>"`  but not between `"<String>" << AnyOtherType` or `AnyOtherType << "<String>"`

This might be considered too much for a change, somehow breaking compatibility but I feel it's wrong and really plays to the contempt people have towards `clang-format` that it goes off and formats things how it wants to without obeying its own line limit rules.

This may need a configuration switch as it's likely to cause clang-format changes in already formatted code (as here with UnwrappedLineParser)  (Polly also breaks)

...I also noticed no unit tests broke when I made this change...


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80950

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -15597,6 +15597,13 @@
                "aaaallvm::outs()\n    <<");
 }
 
+TEST_F(FormatTest, TreatLessLessStringNormally) {
+  verifyFormat("os << \"from\" << \"asdf\";");
+  verifyFormat("os << \"from\" << 1;");
+  verifyFormat("os << \"from\" << 1 << \"foo\";");
+  verifyFormat("os << \"from\" << 1 << \"foo\" << \"bar\";");
+}
+
 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
   std::string code = "#if A\n"
                      "#if B\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2741,8 +2741,7 @@
   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
                                                     E = Line.Tokens.end();
        I != E; ++I) {
-    llvm::dbgs() << I->Tok->Tok.getName() << "["
-                 << "T=" << I->Tok->getType()
+    llvm::dbgs() << I->Tok->Tok.getName() << "[" << "T=" << I->Tok->getType()
                  << ", OC=" << I->Tok->OriginalColumn << "] ";
   }
   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3479,10 +3479,6 @@
     return true;
   if (Right.Previous->IsUnterminatedLiteral)
     return true;
-  if (Right.is(tok::lessless) && Right.Next &&
-      Right.Previous->is(tok::string_literal) &&
-      Right.Next->is(tok::string_literal))
-    return true;
   if (Right.Previous->ClosesTemplateDeclaration &&
       Right.Previous->MatchingParen &&
       Right.Previous->MatchingParen->NestingLevel == 0 &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80950.267713.patch
Type: text/x-patch
Size: 2001 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200601/3c3cb3bd/attachment-0001.bin>


More information about the cfe-commits mailing list