diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/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``. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/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 diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1085,9 +1085,11 @@ if (FormatTok->Tok.is(tok::string_literal)) { nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { - if (Style.BraceWrapping.AfterExternBlock) { + if (Style.BraceWrapping.AfterExternBlock && Style.BraceWrapping.AfterNamespace) { addUnwrappedLine(); parseBlock(/*MustBeDeclaration=*/true); + } else if (Style.BraceWrapping.AfterExternBlock && Style.BraceWrapping.AfterNamespace == false) { + parseBlock(/*MustBeDeclaration=*/false); } else { parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); }