[PATCH] D43312: [clang-format] fix handling of consecutive unary operators

Kevin Lee via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 12:15:28 PST 2018


kevinl created this revision.
kevinl added a reviewer: djasper.
Herald added subscribers: cfe-commits, klimek.

C++ code that used to be formatted as `if (! + object) {` is now formatted as `if (!+object) {`
(we have a particular object in our codebase where unary `operator+` is overloaded to return the underlying value, which in this case is a `bool`)

We still preserve the TypeScript behavior where `!` is a trailing non-null operator. (This is already tested by an existing unit test in `FormatTestJS.cpp`)

It doesn't appear like handling of consecutive unary operators are tested in general? So I added another test for completeness


Repository:
  rC Clang

https://reviews.llvm.org/D43312

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


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5655,6 +5655,8 @@
   verifyFormat("(a->f())++;");
   verifyFormat("a[42]++;");
   verifyFormat("if (!(a->f())) {\n}");
+  verifyFormat("if (!+i) {\n}");
+  verifyFormat("~&a;");
 
   verifyFormat("a-- > b;");
   verifyFormat("b ? -a : c;");
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1493,7 +1493,8 @@
       return TT_UnaryOperator;
 
     if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator) &&
-        !PrevToken->is(tok::exclaim))
+        !(PrevToken->is(tok::exclaim) &&
+          Style.Language == FormatStyle::LK_JavaScript))
       // There aren't any trailing unary operators except for TypeScript's
       // non-null operator (!). Thus, this must be squence of leading operators.
       return TT_UnaryOperator;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43312.134288.patch
Type: text/x-patch
Size: 1046 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180214/90ffdb9d/attachment.bin>


More information about the cfe-commits mailing list