[PATCH] D95168: [clang-format] Add Insert/Remove Braces option

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 4 05:33:36 PDT 2021


MyDeveloperDay updated this revision to Diff 384715.
MyDeveloperDay retitled this revision from "[clang-format] Add InsertBraces option" to "[clang-format] Add Insert/Remove Braces option".
MyDeveloperDay edited the summary of this revision.
MyDeveloperDay added a comment.

This is very much a WIP, updating the patch (as hit a waypoint), and would like others to have a chance to comment on possible configuration options

- move the options to a struct.
- handle non removal in the presence of a comment
- brining in the concept of handling different control statement types differently (i.e. you might want braces removed only from if statements but not the others)
- add unit tests to codify (https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements)

  AutomaticBraces:
      AfterIf: RemoveNoComment
      AfterElse: RemoveNoComment
      AfterFor: Remove
      AfterWhile: Remove
      AfterDo: Remove

Not completely sure  I like the `RemoveNoComment`, we could handle the non removal if there is a comment case globally (same with the "scope depth") e.g.

And like BreakBeforeBraces I sort of feel that we might want to have some predefines i.e. (LLVM/Custom/Gnu?)

At present its very much Insert or Remove, still to add tests for selective insertion, but I wanted to discuss the configuration options first.

  AutomaticBraces: LLVM/Custom
  AutomaticBracesOption:
      AfterIf: Remove
      AfterElse: Remove
      AfterFor: Insert
      AfterWhile: Insert
      AfterDo: Insert
      RemovalIfCommentPresent:false
      MaxScopeDepth: 2

`RemoveNoComment` is the concept of handling a comment after the if() differently

  if (x) {
     // comment
     return x;
  }
  `

I have also run this on some of the sections of LLVM code (Basic/Lexer/Format) to try and catch any potential code change issues.

Also `dogfooded it` by formatting BraceInserter.cpp.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95168/new/

https://reviews.llvm.org/D95168

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/BraceInserter.cpp
  clang/lib/Format/BraceInserter.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/Format.cpp
  clang/unittests/Format/BraceInserterTests.cpp
  clang/unittests/Format/CMakeLists.txt
  clang/unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95168.384715.patch
Type: text/x-patch
Size: 50950 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211104/36e5a92c/attachment-0001.bin>


More information about the cfe-commits mailing list