From 3362fa59ec406ba1eebe9e9ee59a04b675ef2439 Mon Sep 17 00:00:00 2001 From: mydeveloperday Date: Sat, 18 Dec 2021 14:10:14 +0000 Subject: [PATCH] [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 --- clang/lib/Format/UnwrappedLineParser.cpp | 23 +++++++------ clang/unittests/Format/FormatTest.cpp | 44 ++++++++++++++++++++---- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 9ea0db812486..856efbd91cb0 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1279,17 +1279,18 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { if (FormatTok->Tok.is(tok::string_literal)) { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { - if (!Style.IndentExternBlock) { - if (Style.BraceWrapping.AfterExternBlock) { - addUnwrappedLine(); - } - unsigned AddLevels = Style.BraceWrapping.AfterExternBlock ? 1u : 0u; - parseBlock(/*MustBeDeclaration=*/true, AddLevels); - } else { - unsigned AddLevels = - Style.IndentExternBlock == FormatStyle::IEBS_Indent ? 1u : 0u; - parseBlock(/*MustBeDeclaration=*/true, AddLevels); - } + if (Style.BraceWrapping.AfterExternBlock) + addUnwrappedLine(); + // Either we indent or for backwards compatibility we follow the + // AfterExternBlock style. + unsigned AddLevels = + (Style.IndentExternBlock == FormatStyle::IEBS_Indent) || + (Style.BraceWrapping.AfterExternBlock && + Style.IndentExternBlock == + FormatStyle::IEBS_AfterExternBlock) + ? 1u + : 0u; + parseBlock(/*MustBeDeclaration=*/true, AddLevels); addUnwrappedLine(); return; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c2a5fbb5c0d9..2a9de85bf453 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3835,36 +3835,66 @@ TEST_F(FormatTest, IndentExternBlockStyle) { Style.IndentWidth = 2; Style.IndentExternBlock = FormatStyle::IEBS_Indent; - verifyFormat("extern \"C\" { /*9*/\n}", Style); + verifyFormat("extern \"C\" { /*9*/\n" + "}", + Style); verifyFormat("extern \"C\" {\n" " int foo10();\n" "}", Style); Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; - verifyFormat("extern \"C\" { /*11*/\n}", Style); + verifyFormat("extern \"C\" { /*11*/\n" + "}", + Style); verifyFormat("extern \"C\" {\n" "int foo12();\n" "}", Style); - Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; Style.BreakBeforeBraces = FormatStyle::BS_Custom; 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" " int foo14();\n" "}", Style); - Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; - Style.BreakBeforeBraces = FormatStyle::BS_Custom; 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" "int foo16();\n" "}", 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) {