From 58906676e5075e2f416ca69cc38afdc88c7d0bd6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 3 Sep 2014 07:37:29 +0000 Subject: [PATCH] clang-format: Add an option 'SpaceAfterCStyleCast'. This permits to add a space after closing parenthesis of a C-style cast. Defaults to false to preserve old behavior. Fixes llvm.org/PR19982. Before: (int)i; After: (int) i; Patch by Marek Kurdej. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217022 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/ClangFormatStyleOptions.rst | 3 +++ include/clang/Format/Format.h | 4 ++++ lib/Format/Format.cpp | 2 ++ lib/Format/TokenAnnotator.cpp | 5 +++-- unittests/Format/FormatTest.cpp | 33 ++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index 0730a99ead..332fe3becd 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -411,6 +411,9 @@ the configuration (without a prefix: ``Auto``). **SpacesInCStyleCastParentheses** (``bool``) If ``true``, spaces may be inserted into C style casts. +**SpaceAfterCStyleCast** (``bool``) + If ``true``, a space may be inserted after C style casts. + **SpacesInContainerLiterals** (``bool``) If ``true``, spaces are inserted inside container literals (e.g. ObjC and Javascript array and dict literals). diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 96eafada6e..8ee657ae07 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -324,6 +324,9 @@ struct FormatStyle { /// \brief If \c true, spaces may be inserted into C style casts. bool SpacesInCStyleCastParentheses; + /// \brief If \c true, a space may be inserted after C style casts. + bool SpaceAfterCStyleCast; + /// \brief Different ways to put a space before opening parentheses. enum SpaceBeforeParensOptions { /// Never put a space before opening parentheses. @@ -422,6 +425,7 @@ struct FormatStyle { SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && SpacesInContainerLiterals == R.SpacesInContainerLiterals && SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses && + SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceBeforeParens == R.SpaceBeforeParens && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && ContinuationIndentWidth == R.ContinuationIndentWidth && diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 432da61d85..a623ae1c71 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -228,6 +228,7 @@ template <> struct MappingTraits { IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); IO.mapOptional("SpacesInCStyleCastParentheses", Style.SpacesInCStyleCastParentheses); + IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); IO.mapOptional("SpacesInContainerLiterals", Style.SpacesInContainerLiterals); IO.mapOptional("SpaceBeforeAssignmentOperators", @@ -351,6 +352,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpaceInEmptyParentheses = false; LLVMStyle.SpacesInContainerLiterals = true; LLVMStyle.SpacesInCStyleCastParentheses = false; + LLVMStyle.SpaceAfterCStyleCast = false; LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpacesInAngles = false; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 8b62c97a9d..3b92c10fd8 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1641,9 +1641,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Tok.getNextNonComment() && Tok.Type != TT_ObjCMethodExpr && !Tok.Previous->is(tok::question) && (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals); - if (Tok.Previous->Type == TT_UnaryOperator || - Tok.Previous->Type == TT_CastRParen) + if (Tok.Previous->Type == TT_UnaryOperator) return Tok.Type == TT_BinaryOperator; + if (Tok.Previous->Type == TT_CastRParen) + return Style.SpaceAfterCStyleCast || Tok.Type == TT_BinaryOperator; if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) { return Tok.Type == TT_TemplateCloser && Tok.Previous->Type == TT_TemplateCloser && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 451e834269..6f00acf38f 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -7721,6 +7721,38 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) { "default:\n" " break;\n" "}", Spaces); + + Spaces.SpaceAfterCStyleCast = true; + verifyFormat("call(x, y, z);", Spaces); + verifyFormat("while (( bool ) 1)\n" + " continue;", + Spaces); + verifyFormat("for (;;)\n" + " continue;", + Spaces); + verifyFormat("if (true)\n" + " f( );\n" + "else if (true)\n" + " f( );", + Spaces); + verifyFormat("do {\n" + " do_something(( int ) i);\n" + "} while (something( ));", + Spaces); + verifyFormat("switch (x) {\n" + "default:\n" + " break;\n" + "}", + Spaces); + Spaces.SpacesInCStyleCastParentheses = false; + Spaces.SpaceAfterCStyleCast = true; + verifyFormat("while ((bool) 1)\n" + " continue;", + Spaces); + verifyFormat("do {\n" + " do_something((int) i);\n" + "} while (something( ));", + Spaces); } TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { @@ -8306,6 +8338,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(SpaceInEmptyParentheses); CHECK_PARSE_BOOL(SpacesInContainerLiterals); CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); + CHECK_PARSE_BOOL(SpaceAfterCStyleCast); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); } -- 2.40.0