diff options
| author | Alexander Kornienko <alexfh@google.com> | 2013-12-10 10:18:34 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2013-12-10 10:18:34 +0000 |
| commit | 229328d0e6b91cd5ea78e3d79ec9296a2ac57eea (patch) | |
| tree | be06f4940d7502f8410161e5bbfd12a8d9d52b03 /lib/Format/Format.cpp | |
| parent | 1c09d698dc963493d4cc6c5ac65124b62f497f82 (diff) | |
| download | clang-229328d0e6b91cd5ea78e3d79ec9296a2ac57eea.tar.gz clang-229328d0e6b91cd5ea78e3d79ec9296a2ac57eea.tar.bz2 clang-229328d0e6b91cd5ea78e3d79ec9296a2ac57eea.tar.xz | |
Support GNU style rule to put a space before opening parenthesis.
Summary:
The rule from the GNU style states:
"We find it easier to read a program when it has spaces before the open-parentheses and after the commas."
http://www.gnu.org/prep/standards/standards.html#index-spaces-before-open_002dparen
This patch makes clang-format adds an option to put spaces before almost all open parentheses, except the cases, where different behavior is dictated by the style rules or language syntax:
* preprocessor:
** function-like macro definitions can't have a space between the macro name and the parenthesis;
** `#if defined(...)` can have a space, but it seems, that it's more frequently used without a space in GCC, for example;
* never add spaces after unary operators;
* adding spaces between two opening parentheses is controlled with the `SpacesInParentheses` option;
* never add spaces between `[` and `(` (there's no option yet).
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2326
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196901 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
| -rw-r--r-- | lib/Format/Format.cpp | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 26a320b05a..60d323d2a9 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -90,6 +90,24 @@ struct ScalarEnumerationTraits< } }; +template <> +struct ScalarEnumerationTraits< + clang::format::FormatStyle::SpaceBeforeParensOptions> { + static void + enumeration(IO &IO, + clang::format::FormatStyle::SpaceBeforeParensOptions &Value) { + IO.enumCase(Value, "Never", clang::format::FormatStyle::SBPO_Never); + IO.enumCase(Value, "ControlStatements", + clang::format::FormatStyle::SBPO_ControlStatements); + IO.enumCase(Value, "Always", clang::format::FormatStyle::SBPO_Always); + + // For backward compatibility. + IO.enumCase(Value, "false", clang::format::FormatStyle::SBPO_Never); + IO.enumCase(Value, "true", + clang::format::FormatStyle::SBPO_ControlStatements); + } +}; + template <> struct MappingTraits<clang::format::FormatStyle> { static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { if (IO.outputting()) { @@ -179,11 +197,16 @@ template <> struct MappingTraits<clang::format::FormatStyle> { IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); IO.mapOptional("SpacesInCStyleCastParentheses", Style.SpacesInCStyleCastParentheses); - IO.mapOptional("SpaceAfterControlStatementKeyword", - Style.SpaceAfterControlStatementKeyword); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth); + + // For backward compatibility. + if (!IO.outputting()) { + IO.mapOptional("SpaceAfterControlStatementKeyword", + Style.SpaceBeforeParens); + } + IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); } }; @@ -266,7 +289,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.SpacesInParentheses = false; LLVMStyle.SpaceInEmptyParentheses = false; LLVMStyle.SpacesInCStyleCastParentheses = false; - LLVMStyle.SpaceAfterControlStatementKeyword = true; + LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.ContinuationIndentWidth = 4; LLVMStyle.SpacesInAngles = false; @@ -315,7 +338,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.SpacesInParentheses = false; GoogleStyle.SpaceInEmptyParentheses = false; GoogleStyle.SpacesInCStyleCastParentheses = false; - GoogleStyle.SpaceAfterControlStatementKeyword = true; + GoogleStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; GoogleStyle.SpaceBeforeAssignmentOperators = true; GoogleStyle.ContinuationIndentWidth = 4; GoogleStyle.SpacesInAngles = false; |
