[clang-format] Add IndentCaseBlocks option
Summary:
The documentation for IndentCaseLabels claimed that the "Switch
statement body is always indented one level more than case labels". This
is technically false for the code block immediately following the label.
Its closing bracket aligns with the start of the label.
If the case label are not indented, it leads to a style where the
closing bracket of the block aligns with the closing bracket of the
switch statement, which can be hard to parse.
This change introduces a new option, IndentCaseBlocks, which when true
treats the block as a scope block (which it technically is).
(Note: regenerated ClangFormatStyleOptions.rst using tools/dump_style.py)
Reviewed By: MyDeveloperDay
Patch By: capn
Tags: #clang-format, #clang
Differential Revision: https://reviews.llvm.org/D72276
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 18cdc54..fd77e1e 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -1605,12 +1605,36 @@
``ClassImpl.hpp`` would not have the main include file put on top
before any other include.
+**IndentCaseBlocks** (``bool``)
+ Indent case label blocks one level from the case label.
+
+ When ``false``, the block following the case label uses the same
+ indentation level as for the case label, treating the case label the same
+ as an if-statement.
+ When ``true``, the block gets indented as a scope block.
+
+ .. code-block:: c++
+
+ false: true:
+ switch (fool) { vs. switch (fool) {
+ case 1: { case 1:
+ bar(); {
+ } break; bar();
+ default: { }
+ plop(); break;
+ } default:
+ } {
+ plop();
+ }
+ }
+
**IndentCaseLabels** (``bool``)
Indent case labels one level from the switch statement.
When ``false``, use the same indentation level as for the switch
statement. Switch statement body is always indented one level more than
- case labels.
+ case labels (except the first block following the case label, which
+ itself indents the code - unless IndentCaseBlocks is enabled).
.. code-block:: c++
@@ -2329,7 +2353,14 @@
x = ( int32 )y vs. x = (int32)y
**SpacesInConditionalStatement** (``bool``)
- If ``true``, spaces will be inserted around if/for/while (and similar) conditions.
+ If ``true``, spaces will be inserted around if/for/switch/while
+ conditions.
+
+ .. code-block:: c++
+
+ true: false:
+ if ( a ) { ... } vs. if (a) { ... }
+ while ( i < 5 ) { ... } while (i < 5) { ... }
**SpacesInContainerLiterals** (``bool``)
If ``true``, spaces are inserted inside container literals (e.g.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 35132ce..76a1d74 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -177,6 +177,25 @@
------------
+- Option ``IndentCaseBlocks`` has been added to support treating the block
+ following a switch case label as a scope block which gets indented itself.
+ It helps avoid having the closing bracket align with the switch statement's
+ closing bracket (when ``IndentCaseLabels`` is ``false``).
+
+ .. code-block:: c++
+
+ switch (fool) { vs. switch (fool) {
+ case 1: case 1: {
+ { bar();
+ bar(); } break;
+ } default: {
+ break; plop();
+ default: }
+ { }
+ plop();
+ }
+ }
+
libclang
--------