]> granicus.if.org Git - clang/commitdiff
[Format] Add format check for throwing negative numbers
authorBrian Gesiak <modocache@gmail.com>
Fri, 18 Oct 2019 16:59:02 +0000 (16:59 +0000)
committerBrian Gesiak <modocache@gmail.com>
Fri, 18 Oct 2019 16:59:02 +0000 (16:59 +0000)
Summary:
The code `throw -1;` is currently formatted by clang-format as
`throw - 1;`. This diff adds a fix for this edge case and a test to check
for this in the future.

For context, I am looking into a related bug in the clang-formatting of
coroutine keywords: `co_yield -1;` is also reformatted in this manner
as `co_yield - 1;`. A later diff will add these changes and tests for the
`co_yield` and `co_return` keywords.

Patch by Jonathan Thomas (jonathoma)!

Reviewers: modocache, sammccall, Quuxplusone

Reviewed By: sammccall

Subscribers: cfe-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D69144

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@375258 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7aa1f378f0bfb6cc9930c3ec8602c1d9909add9c..1ed35597d075de4c44aaac772396588dd85f7a31 100644 (file)
@@ -1757,7 +1757,7 @@ private:
     // Use heuristics to recognize unary operators.
     if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
                            tok::question, tok::colon, tok::kw_return,
-                           tok::kw_case, tok::at, tok::l_brace))
+                           tok::kw_case, tok::at, tok::l_brace, tok::kw_throw))
       return TT_UnaryOperator;
 
     // There can't be two consecutive binary operators.
index 19269b241868fd8806ad20282c21af666dd5e4a7..e0ebef1f7cedea55d3802eebf02cf37ef4c5dfed 100644 (file)
@@ -6912,6 +6912,7 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
   verifyFormat("alignof(char);", getGoogleStyle());
 
   verifyFormat("return -1;");
+  verifyFormat("throw -1;");
   verifyFormat("switch (a) {\n"
                "case -1:\n"
                "  break;\n"