Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -935,6 +935,28 @@ extern "C" { int foo(); } + + true: + #ifdef __cplusplus + extern "C" { + #endif + + void f(void); + + #ifdef __cplusplus + } + #endif + + false: + #ifdef __cplusplus + extern "C" { + #endif + + void f(void); + + #ifdef __cplusplus + } + #endif * ``bool BeforeCatch`` Wrap before ``catch``. Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -977,6 +977,29 @@ /// } catch () { /// } /// \endcode + /// \code + /// true: + /// #ifdef __cplusplus + /// extern "C" { + /// #endif + /// + /// void f(void); + /// + /// #ifdef __cplusplus + /// } + /// #endif + /// + /// false: + /// #ifdef __cplusplus + /// extern "C" { + /// #endif + /// + /// void f(void); + /// + /// #ifdef __cplusplus + /// } + /// #endif + /// \endcode bool BeforeCatch; /// Wrap before ``else``. /// \code Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1086,7 +1086,6 @@ nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { if (Style.BraceWrapping.AfterExternBlock) { - addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true); } else { parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -2462,14 +2462,18 @@ Style.BraceWrapping.AfterExternBlock = true; Style.BraceWrapping.SplitEmptyRecord = false; - verifyFormat("extern \"C\"\n" - "{}", - Style); - verifyFormat("extern \"C\"\n" - "{\n" + verifyFormat("extern \"C\" {}", Style); + verifyFormat("extern \"C\" {\n" " int foo();\n" "}", Style); + + Style.BraceWrapping.AfterExternBlock = false; + verifyFormat("extern \"C\" {}", Style); + verifyFormat("extern \"C\" {\n" + "int foo();\n" + "}", + Style); } TEST_F(FormatTest, FormatsInlineASM) {