Author: krasimir Date: Tue Mar 6 05:56:28 2018 New Revision: 326792 URL: http://llvm.org/viewvc/llvm-project?rev=326792&view=rev Log: [clang-format] fix handling of consecutive unary operators
Summary: 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 Patch contributed by @kevinl! Reviewers: krasimir Reviewed By: krasimir Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43312 Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp cfe/trunk/unittests/Format/FormatTest.cpp cfe/trunk/unittests/Format/FormatTestJS.cpp Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=326792&r1=326791&r2=326792&view=diff ============================================================================== --- cfe/trunk/lib/Format/TokenAnnotator.cpp (original) +++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Mar 6 05:56:28 2018 @@ -1531,10 +1531,8 @@ private: if (!PrevToken) return TT_UnaryOperator; - if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator) && - !PrevToken->is(tok::exclaim)) - // There aren't any trailing unary operators except for TypeScript's - // non-null operator (!). Thus, this must be squence of leading operators. + if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator)) + // This must be a sequence of leading unary operators. return TT_UnaryOperator; // Use heuristics to recognize unary operators. Modified: cfe/trunk/unittests/Format/FormatTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=326792&r1=326791&r2=326792&view=diff ============================================================================== --- cfe/trunk/unittests/Format/FormatTest.cpp (original) +++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Mar 6 05:56:28 2018 @@ -5655,6 +5655,8 @@ TEST_F(FormatTest, UnderstandsUnaryOpera verifyFormat("(a->f())++;"); verifyFormat("a[42]++;"); verifyFormat("if (!(a->f())) {\n}"); + verifyFormat("if (!+i) {\n}"); + verifyFormat("~&a;"); verifyFormat("a-- > b;"); verifyFormat("b ? -a : c;"); Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=326792&r1=326791&r2=326792&view=diff ============================================================================== --- cfe/trunk/unittests/Format/FormatTestJS.cpp (original) +++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Mar 6 05:56:28 2018 @@ -2133,6 +2133,7 @@ TEST_F(FormatTestJS, NonNullAssertionOpe verifyFormat("let x = foo!.bar();\n"); verifyFormat("let x = foo ? bar! : baz;\n"); verifyFormat("let x = !foo;\n"); + verifyFormat("if (!+a) {\n}"); verifyFormat("let x = foo[0]!;\n"); verifyFormat("let x = (foo)!;\n"); verifyFormat("let x = x(foo!);\n"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits