dpayne created this revision. dpayne added reviewers: klimek, djasper. Herald added subscribers: cfe-commits, mgorny.
Hi, This is another attempt at fixing the issue describe here https://reviews.llvm.org/D22505. Some code bases will have an extra indent for member variables and functions, while the access modifiers are only indented once. For example, class MyClass { public: // 1 level of indent int value2; // 2 levels of indent private: //1 level of indent int value3; // 2 levels of indent }; The current solution is to use a combination of increasing indent width and setting AccessModifierOffset to a offset. While this might work for some code bases, it has the downside of changing the indent level across the whole project. To fix this I added AdditionalIndentClassBlock, which when true will indent everything within a class block twice. This then could be used with AccessModifierOffset to achieve the desired formatting. By default it is off. Going off the previous ticket, my goal here was to come up with a way to get this kind of formatting with the least amount of changes to the rest of the formatting rules. Repository: rC Clang https://reviews.llvm.org/D51120 Files: docs/ClangFormatStyleOptions.rst lib/Format/Format.cpp lib/Format/UnwrappedLineParser.cpp lib/Format/UnwrappedLineParser.h unittests/Format/CMakeLists.txt Index: unittests/Format/CMakeLists.txt =================================================================== --- unittests/Format/CMakeLists.txt +++ unittests/Format/CMakeLists.txt @@ -5,6 +5,7 @@ add_clang_unittest(FormatTests CleanupTest.cpp FormatTest.cpp + FormatTestClassIndent.cpp FormatTestComments.cpp FormatTestJS.cpp FormatTestJava.cpp Index: lib/Format/UnwrappedLineParser.h =================================================================== --- lib/Format/UnwrappedLineParser.h +++ lib/Format/UnwrappedLineParser.h @@ -88,7 +88,7 @@ void parseFile(); void parseLevel(bool HasOpeningBrace); void parseBlock(bool MustBeDeclaration, bool AddLevel = true, - bool MunchSemi = true); + bool MunchSemi = true, bool ClassBlock = false); void parseChildBlock(); void parsePPDirective(); void parsePPDefine(); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -520,7 +520,7 @@ } void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, - bool MunchSemi) { + bool MunchSemi, bool ClassBlock) { assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && "'{' or macro block token expected"); const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); @@ -546,6 +546,9 @@ MustBeDeclaration); if (AddLevel) ++Line->Level; + + if (Style.AdditionalIndentClassBlock && ClassBlock) + ++Line->Level; parseLevel(/*HasOpeningBrace=*/true); if (eof()) @@ -2126,7 +2129,7 @@ addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, - /*MunchSemi=*/false); + /*MunchSemi=*/false, /*ClassBlock*/ true); } } // There is no addUnwrappedLine() here so that we fall through to parsing a Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -313,6 +313,7 @@ } IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); + IO.mapOptional("AdditionalIndentClassBlock", Style.AdditionalIndentClassBlock); IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); @@ -621,6 +622,7 @@ FormatStyle LLVMStyle; LLVMStyle.Language = FormatStyle::LK_Cpp; LLVMStyle.AccessModifierOffset = -2; + LLVMStyle.AdditionalIndentClassBlock = false; LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; LLVMStyle.AlignOperands = true; Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -150,6 +150,9 @@ **AccessModifierOffset** (``int``) The extra indent or outdent of access modifiers, e.g. ``public:``. +**AdditionalIndentClassBlock** (``bool``) + If ``true``, adds an additional level of indention for class blocks. + **AlignAfterOpenBracket** (``BracketAlignmentStyle``) If ``true``, horizontally aligns arguments after an open bracket.
Index: unittests/Format/CMakeLists.txt =================================================================== --- unittests/Format/CMakeLists.txt +++ unittests/Format/CMakeLists.txt @@ -5,6 +5,7 @@ add_clang_unittest(FormatTests CleanupTest.cpp FormatTest.cpp + FormatTestClassIndent.cpp FormatTestComments.cpp FormatTestJS.cpp FormatTestJava.cpp Index: lib/Format/UnwrappedLineParser.h =================================================================== --- lib/Format/UnwrappedLineParser.h +++ lib/Format/UnwrappedLineParser.h @@ -88,7 +88,7 @@ void parseFile(); void parseLevel(bool HasOpeningBrace); void parseBlock(bool MustBeDeclaration, bool AddLevel = true, - bool MunchSemi = true); + bool MunchSemi = true, bool ClassBlock = false); void parseChildBlock(); void parsePPDirective(); void parsePPDefine(); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -520,7 +520,7 @@ } void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, - bool MunchSemi) { + bool MunchSemi, bool ClassBlock) { assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && "'{' or macro block token expected"); const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); @@ -546,6 +546,9 @@ MustBeDeclaration); if (AddLevel) ++Line->Level; + + if (Style.AdditionalIndentClassBlock && ClassBlock) + ++Line->Level; parseLevel(/*HasOpeningBrace=*/true); if (eof()) @@ -2126,7 +2129,7 @@ addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, - /*MunchSemi=*/false); + /*MunchSemi=*/false, /*ClassBlock*/ true); } } // There is no addUnwrappedLine() here so that we fall through to parsing a Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -313,6 +313,7 @@ } IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); + IO.mapOptional("AdditionalIndentClassBlock", Style.AdditionalIndentClassBlock); IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket); IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); @@ -621,6 +622,7 @@ FormatStyle LLVMStyle; LLVMStyle.Language = FormatStyle::LK_Cpp; LLVMStyle.AccessModifierOffset = -2; + LLVMStyle.AdditionalIndentClassBlock = false; LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; LLVMStyle.AlignOperands = true; Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -150,6 +150,9 @@ **AccessModifierOffset** (``int``) The extra indent or outdent of access modifiers, e.g. ``public:``. +**AdditionalIndentClassBlock** (``bool``) + If ``true``, adds an additional level of indention for class blocks. + **AlignAfterOpenBracket** (``BracketAlignmentStyle``) If ``true``, horizontally aligns arguments after an open bracket.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits