[PATCH] D37143: [clang-format] Fixed missing enter before bracket in typedef enum and extern

Pawel Maciocha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 06:03:18 PDT 2017


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

Bug: https://bugs.llvm.org/show_bug.cgi?id=34016

**Problem:**

Clang format does not allow the flag **BraceWrapping.AfterEnum** control the case when our **enum** is preceded by **typedef** keyword (what is common in C language). 
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 case to the **"AfterEnum"** flag when our enum does not start a line - is preceded by **typedef** keyword.
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:**

**BEFORE:**

  typedef enum 
  {
      a,
      b,
      c
  } SomeEnum;
  
  extern "C" 
  {
  #include <SomeInclude.h>
  }

**AFTER:**

  typedef enum 
  {
      a,
      b,
      c
  } SomeEnum;
  
  extern "C" 
  {
  #include <SomeInclude.h>
  }

**Remains the same!**


https://reviews.llvm.org/D37143

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


Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -986,6 +986,8 @@
     if (FormatTok->Tok.is(tok::string_literal)) {
       nextToken();
       if (FormatTok->Tok.is(tok::l_brace)) {
+        if (isOnNewLine(*FormatTok))
+          MustBreakBeforeNextToken = true;
         parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
         addUnwrappedLine();
         return;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2647,6 +2647,7 @@
     return Right.HasUnescapedNewline;
   if (isAllmanBrace(Left) || isAllmanBrace(Right))
     return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
+           (Line.startsWith(tok::kw_typedef, tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
            (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
            (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
   if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37143.112683.patch
Type: text/x-patch
Size: 1225 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170825/53ccd39a/attachment-0001.bin>


More information about the cfe-commits mailing list