[PATCH] D26953: clang-format: handle formatting on constexpr if

Visoiu Mistrih Francis via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 21 19:20:12 PST 2016


thegameg created this revision.
thegameg added reviewers: djasper, mprobst, ioeric, klimek.
thegameg added a subscriber: cfe-commits.

c++1z adds the following constructions to the language:

if constexpr (cond)

  statement1;

else if constexpr (cond)

  statement2;

else if constexpr (cond)

  statement3;

else

  statement4;

so clang-format needs to accept the `constexpr` keyword after the `if`
keyword.

- lib/Format/TokenAnnotator.cpp: Handle Style.SpaceBeforeParens.
- lib/Format/UnwrappedLineParser.cpp: Skip the token when parsing

`ifThenElse`.

- unittests/Format/FormatTest.cpp: Tests.


https://reviews.llvm.org/D26953

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


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -11715,6 +11715,17 @@
                    "/* comment 3 */         \\\n",
                    getLLVMStyleWithColumns(40)));
 }
+
+TEST_F(FormatTest, ConstexprIf) {
+  EXPECT_EQ("if constexpr (3 > 4)",
+            format("if constexpr(3 > 4)",
+                   getLLVMStyle()));
+  EXPECT_EQ("if constexpr (3 > 4)",
+            format("if \n"
+                   "  constexpr(3 > 4)",
+                   getLLVMStyle()));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -1430,6 +1430,8 @@
 void UnwrappedLineParser::parseIfThenElse() {
   assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
   nextToken();
+  if (FormatTok->Tok.is(tok::kw_constexpr))
+    nextToken();
   if (FormatTok->Tok.is(tok::l_paren))
     parseParens();
   bool NeedsUnwrappedLine = false;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2119,9 +2119,9 @@
       return true;
     return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
            (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
-            (Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while,
-                          tok::kw_switch, tok::kw_case, TT_ForEachMacro,
-                          TT_ObjCForIn) ||
+            (Left.isOneOf(tok::kw_if, tok::kw_constexpr, tok::pp_elif,
+                          tok::kw_for, tok::kw_while, tok::kw_switch,
+                          tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
              (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
                            tok::kw_new, tok::kw_delete) &&
               (!Left.Previous || Left.Previous->isNot(tok::period))))) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26953.78821.patch
Type: text/x-patch
Size: 2155 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161122/ffbea682/attachment.bin>


More information about the cfe-commits mailing list