[PATCH] D37260: [clang-format] Fixed extern C brace wrapping

Pawel Maciocha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 29 04:41:19 PDT 2017

PriMee created this revision.
Herald added a subscriber: klimek.

Bug: https://bugs.llvm.org/show_bug.cgi?id=34016 - **"extern C part"**


Due to the lack of "brace wrapping extern" flag, clang format does parse the block after **extern** keyword moving the opening bracket to the header line **always**!

**Patch description:**

Added if statement handling the case when our **"extern block"** has the opening bracket in "non-header" line. Then forcing break before bracket.

**After fix:**


  BreakBeforeBraces: Custom
  BraceWrapping: { 
  AfterClass: true, AfterControlStatement: true, AfterEnum: true, AfterFunction: true, AfterNamespace: false, AfterStruct: true, AfterUnion: true, BeforeCatch: true, BeforeElse: true 


  extern "C" 
  #include <SomeInclude.h>


  extern "C" 
  #include <SomeInclude.h>

**Remains the same!**

There is no brace wrapping flag that let us control opening brace's position. In case of other keywords (class, function, control statement etc.) we have opportunity to decide how should it look like. Here, we can't do it similarly. What we want is leaving braces **unformatted** (leave them as in the input), but what's more we still want to call **parseBlock** function. The only option is to set **MustBreakBefore** flag manually (only when needed, when the left brace is on non-header line, parseBlock does move it by default).



Index: unittests/Format/FormatTest.cpp
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1595,7 +1595,24 @@
-TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
+TEST_F(FormatTest, FormatsExternC) { 
+  verifyFormat("extern \"C\" {\nint a;");
+  verifyFormat("extern \"C\" {};");
+  EXPECT_EQ("extern \"C\" {\n"
+            "int i = 42;\n"
+            "}",
+            format("extern \"C\" {\n"
+                   "int i = 42;\n"
+                   "}"));
+  EXPECT_EQ("extern \"C\"\n" 
+            "{\n"
+            "int i = 42;\n"
+            "}",
+            format("extern \"C\"\n" 
+                   "{\n"
+                   "int i = 42;\n"
+                   "}"));
 TEST_F(FormatTest, FormatsInlineASM) {
   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
Index: lib/Format/UnwrappedLineParser.cpp
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -986,6 +986,8 @@
     if (FormatTok->Tok.is(tok::string_literal)) {
       if (FormatTok->Tok.is(tok::l_brace)) {
+        if (isOnNewLine(*FormatTok))
+          FormatTok->MustBreakBefore = true;
         parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37260.113061.patch
Type: text/x-patch
Size: 1480 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170829/71c87d35/attachment.bin>

More information about the cfe-commits mailing list