mprobst created this revision. mprobst added a reviewer: krasimir. mprobst requested review of this revision. Herald added a project: clang.
In JavaScript, `- -1;` is legal syntax, the language allows unary minus. However the two tokens must not collapse together: `--1` is prefix decrement, i.e. different syntax. Before: - -1; ==> --1; After: - -1; ==> - -1; This change makes no attempt to format this "nicely", given by all likelihood this represents a programming mistake by the user, or odd generated code. The check is not guarded by language: this appears to be a problem in Java as well, and will also be beneficial when formatting syntactically incorrect C++ (e.g. during editing). Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D99495 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTestJS.cpp Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -276,6 +276,12 @@ // ES6 spread operator. verifyFormat("someFunction(...a);"); verifyFormat("var x = [1, ...a, 2];"); + + // "- -1" is legal JS syntax, but must not collapse into "--". + verifyFormat("- -1;", " - -1;"); + verifyFormat("-- -1;", " -- -1;"); + verifyFormat("+ +1;", " + +1;"); + verifyFormat("++ +1;", " ++ +1;"); } TEST_F(FormatTestJS, UnderstandsAmpAmp) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -3370,6 +3370,12 @@ Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; return true; } + // Do not merge "- -" into "--". + if ((Left.isOneOf(tok::minus, tok::minusminus) && + Right.isOneOf(tok::minus, tok::minusminus)) || + (Left.isOneOf(tok::plus, tok::plusplus) && + Right.isOneOf(tok::plus, tok::plusplus))) + return true; if (Left.is(TT_UnaryOperator)) { if (!Right.is(tok::l_paren)) { // The alternative operators for ~ and ! are "compl" and "not".
Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -276,6 +276,12 @@ // ES6 spread operator. verifyFormat("someFunction(...a);"); verifyFormat("var x = [1, ...a, 2];"); + + // "- -1" is legal JS syntax, but must not collapse into "--". + verifyFormat("- -1;", " - -1;"); + verifyFormat("-- -1;", " -- -1;"); + verifyFormat("+ +1;", " + +1;"); + verifyFormat("++ +1;", " ++ +1;"); } TEST_F(FormatTestJS, UnderstandsAmpAmp) { Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -3370,6 +3370,12 @@ Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; return true; } + // Do not merge "- -" into "--". + if ((Left.isOneOf(tok::minus, tok::minusminus) && + Right.isOneOf(tok::minus, tok::minusminus)) || + (Left.isOneOf(tok::plus, tok::plusplus) && + Right.isOneOf(tok::plus, tok::plusplus))) + return true; if (Left.is(TT_UnaryOperator)) { if (!Right.is(tok::l_paren)) { // The alternative operators for ~ and ! are "compl" and "not".
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits