[clang-format] extern with new line brace without indentation

https://github.com/llvm/llvm-project/issues/49804

Interaction between IndentExternBlock and AfterExternBlock means you cannot have AfterExternBlock = true and IndentExternBlock = NoIndent/Indent

This patch resolves that
```
BraceWrapping:
  AfterExternBlock: true
IndentExternBlock: AfterExternBlock
```
Fixes: #49804

Reviewed By: HazardyKnusperkeks, curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D115879
This commit is contained in:
mydeveloperday 2021-12-18 14:10:14 +00:00
parent ff94bd1bc9
commit 3362fa59ec
2 changed files with 49 additions and 18 deletions

View File

@ -1279,17 +1279,18 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
if (FormatTok->Tok.is(tok::string_literal)) { if (FormatTok->Tok.is(tok::string_literal)) {
nextToken(); nextToken();
if (FormatTok->Tok.is(tok::l_brace)) { if (FormatTok->Tok.is(tok::l_brace)) {
if (!Style.IndentExternBlock) { if (Style.BraceWrapping.AfterExternBlock)
if (Style.BraceWrapping.AfterExternBlock) { addUnwrappedLine();
addUnwrappedLine(); // Either we indent or for backwards compatibility we follow the
} // AfterExternBlock style.
unsigned AddLevels = Style.BraceWrapping.AfterExternBlock ? 1u : 0u; unsigned AddLevels =
parseBlock(/*MustBeDeclaration=*/true, AddLevels); (Style.IndentExternBlock == FormatStyle::IEBS_Indent) ||
} else { (Style.BraceWrapping.AfterExternBlock &&
unsigned AddLevels = Style.IndentExternBlock ==
Style.IndentExternBlock == FormatStyle::IEBS_Indent ? 1u : 0u; FormatStyle::IEBS_AfterExternBlock)
parseBlock(/*MustBeDeclaration=*/true, AddLevels); ? 1u
} : 0u;
parseBlock(/*MustBeDeclaration=*/true, AddLevels);
addUnwrappedLine(); addUnwrappedLine();
return; return;
} }

View File

@ -3835,36 +3835,66 @@ TEST_F(FormatTest, IndentExternBlockStyle) {
Style.IndentWidth = 2; Style.IndentWidth = 2;
Style.IndentExternBlock = FormatStyle::IEBS_Indent; Style.IndentExternBlock = FormatStyle::IEBS_Indent;
verifyFormat("extern \"C\" { /*9*/\n}", Style); verifyFormat("extern \"C\" { /*9*/\n"
"}",
Style);
verifyFormat("extern \"C\" {\n" verifyFormat("extern \"C\" {\n"
" int foo10();\n" " int foo10();\n"
"}", "}",
Style); Style);
Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
verifyFormat("extern \"C\" { /*11*/\n}", Style); verifyFormat("extern \"C\" { /*11*/\n"
"}",
Style);
verifyFormat("extern \"C\" {\n" verifyFormat("extern \"C\" {\n"
"int foo12();\n" "int foo12();\n"
"}", "}",
Style); Style);
Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterExternBlock = true; Style.BraceWrapping.AfterExternBlock = true;
verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); Style.IndentExternBlock = FormatStyle::IEBS_Indent;
verifyFormat("extern \"C\"\n"
"{ /*13*/\n"
"}",
Style);
verifyFormat("extern \"C\"\n{\n" verifyFormat("extern \"C\"\n{\n"
" int foo14();\n" " int foo14();\n"
"}", "}",
Style); Style);
Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterExternBlock = false; Style.BraceWrapping.AfterExternBlock = false;
verifyFormat("extern \"C\" { /*15*/\n}", Style); Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
verifyFormat("extern \"C\" { /*15*/\n"
"}",
Style);
verifyFormat("extern \"C\" {\n" verifyFormat("extern \"C\" {\n"
"int foo16();\n" "int foo16();\n"
"}", "}",
Style); Style);
Style.BraceWrapping.AfterExternBlock = true;
verifyFormat("extern \"C\"\n"
"{ /*13*/\n"
"}",
Style);
verifyFormat("extern \"C\"\n"
"{\n"
"int foo14();\n"
"}",
Style);
Style.IndentExternBlock = FormatStyle::IEBS_Indent;
verifyFormat("extern \"C\"\n"
"{ /*13*/\n"
"}",
Style);
verifyFormat("extern \"C\"\n"
"{\n"
" int foo14();\n"
"}",
Style);
} }
TEST_F(FormatTest, FormatsInlineASM) { TEST_F(FormatTest, FormatsInlineASM) {