[PATCH] D43183: clang-format: introduce `CaseBlockIndent` to control indent in switch

Francois Ferrand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 12 05:47:00 PST 2018


Typz created this revision.
Typz added reviewers: krasimir, djasper, klimek.

When a block is started after a case label, clang-format does add extra
indent to the content of this block: the block content is indented one
level (with respect to the switch) while the closing brace is not
indented, and closes at switch level.

This gives the following code:

  switch (x) {
  case A: {
    stuff();
  } break;
  }

This makes it easy to confuse the closing brace of the 'case' with that
the whole 'switch', especially if more code is added after the brace:

  switch (x) {
  case A: {
    stuff();
  }
    moreStuff();
    break;
  }

This patch introduces a new CaseBlockIndent switch, which provides
alternative formatting for these cases:

- `CaseBlockIndent = None` : default behavior, same behavior as before

- `CaseBlockIndent = ClosingBrace` : indent the closing brace to the

same level as its content.

  switch (x) {
  case A: {
    stuff();
    } break;
  }

- `CaseBlockIndent = Block` : add an extra level of indent for the

content of the block.

  switch (x) {
  case A: {
      stuff();
    } break;
  }


Repository:
  rC Clang

https://reviews.llvm.org/D43183

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  lib/Format/UnwrappedLineParser.cpp
  lib/Format/UnwrappedLineParser.h
  unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43183.133841.patch
Type: text/x-patch
Size: 9614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180212/79632677/attachment.bin>


More information about the cfe-commits mailing list