summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-12-10 10:18:34 +0000
committerAlexander Kornienko <alexfh@google.com>2013-12-10 10:18:34 +0000
commitfdca83d487e9916b56a26fdd5eadd7979c34496b (patch)
tree2c7f88fd09b45201504bc3317320c7bfdbba814e /clang/unittests/Format/FormatTest.cpp
parentb2eb3d3177ecc878fc440d912f4e74a4184c44c7 (diff)
downloadbcm5719-llvm-fdca83d487e9916b56a26fdd5eadd7979c34496b.tar.gz
bcm5719-llvm-fdca83d487e9916b56a26fdd5eadd7979c34496b.zip
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 llvm-svn: 196901
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
-rw-r--r--clang/unittests/Format/FormatTest.cpp57
1 files changed, 53 insertions, 4 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e2f8d2fa65c..faaafc2c881 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -2082,7 +2082,8 @@ TEST_F(FormatTest, HashInMacroDefinition) {
}
TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
- verifyFormat("#define A (1)");
+ EXPECT_EQ("#define A (x)", format("#define A (x)"));
+ EXPECT_EQ("#define A(x)", format("#define A(x)"));
}
TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
@@ -6660,9 +6661,9 @@ TEST_F(FormatTest, CalculatesOriginalColumn) {
getLLVMStyle()));
}
-TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
+TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
FormatStyle NoSpace = getLLVMStyle();
- NoSpace.SpaceAfterControlStatementKeyword = false;
+ NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("while(true)\n"
" continue;", NoSpace);
@@ -6679,6 +6680,42 @@ TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
"default:\n"
" break;\n"
"}", NoSpace);
+
+ FormatStyle Space = getLLVMStyle();
+ Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
+
+ verifyFormat("int f ();", Space);
+ verifyFormat("void f (int a, T b) {\n"
+ " while (true)\n"
+ " continue;\n"
+ "}",
+ Space);
+ verifyFormat("if (true)\n"
+ " f ();\n"
+ "else if (true)\n"
+ " f ();",
+ Space);
+ verifyFormat("do {\n"
+ " do_something ();\n"
+ "} while (something ());",
+ Space);
+ verifyFormat("switch (x) {\n"
+ "default:\n"
+ " break;\n"
+ "}",
+ Space);
+ verifyFormat("A::A () : a (1) {}", Space);
+ verifyFormat("void f () __attribute__ ((asdf));", Space);
+ verifyFormat("*(&a + 1);\n"
+ "&((&a)[1]);\n"
+ "a[(b + c) * d];\n"
+ "(((a + 1) * 2) + 3) * 4;",
+ Space);
+ verifyFormat("#define A(x) x", Space);
+ verifyFormat("#define A (x) x", Space);
+ verifyFormat("#if defined(x)\n"
+ "#endif",
+ Space);
}
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
@@ -6988,7 +7025,6 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(SpacesInAngles);
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
- CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
@@ -7020,6 +7056,19 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
+ Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
+ CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
+ FormatStyle::SBPO_Never);
+ CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
+ FormatStyle::SBPO_Always);
+ CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
+ FormatStyle::SBPO_ControlStatements);
+ // For backward compatibility:
+ CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
+ FormatStyle::SBPO_Never);
+ CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
+ FormatStyle::SBPO_ControlStatements);
+
Style.ColumnLimit = 123;
FormatStyle BaseStyle = getLLVMStyle();
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
OpenPOWER on IntegriCloud