From 6cb96e4663d8e61db5bb520e5946c314ff8354a1 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 26 Nov 2014 10:43:58 +0000 Subject: [PATCH] clang-format: Add SFS_Empty to only empty functions on a single line. Activated for and tested by Google's Java style. This fixes llvm.org/PR21667. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@222819 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 2 + lib/Format/Format.cpp | 8 +++- unittests/Format/FormatTest.cpp | 2 + unittests/Format/FormatTestJava.cpp | 58 ++++++++++++----------------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index a04705c36d..41e6985731 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -212,6 +212,8 @@ struct FormatStyle { SFS_None, /// \brief Only merge functions defined inside a class. SFS_Inline, + /// \brief Only merge empty functions. + SFS_Empty, /// \brief Merge all functions fitting on a single line. SFS_All, }; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 8a04571a8c..92086461d4 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -74,6 +74,7 @@ template <> struct ScalarEnumerationTraits { IO.enumCase(Value, "All", FormatStyle::SFS_All); IO.enumCase(Value, "true", FormatStyle::SFS_All); IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline); + IO.enumCase(Value, "Empty", FormatStyle::SFS_Empty); } }; @@ -414,7 +415,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { if (Language == FormatStyle::LK_Java) { GoogleStyle.AlignAfterOpenBracket = false; - GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; + GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; GoogleStyle.ColumnLimit = 100; GoogleStyle.SpaceAfterCStyleCast = true; @@ -639,6 +640,8 @@ public: // If necessary, change to something smarter. bool MergeShortFunctions = Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All || + (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty && + I[1]->First->is(tok::r_brace)) || (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline && TheLine->Level != 0); @@ -768,7 +771,8 @@ private: AnnotatedLine &Line = **I; // Don't merge ObjC @ keywords and methods. - if (Line.First->isOneOf(tok::at, tok::minus, tok::plus)) + if (Style.Language != FormatStyle::LK_Java && + Line.First->isOneOf(tok::at, tok::minus, tok::plus)) return 0; // Check that the current line allows merging. This depends on whether we diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b7ffd95828..3e8101c102 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -8680,6 +8680,8 @@ TEST_F(FormatTest, ParsesConfiguration) { AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); + CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", + AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); // For backward compatibility: diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index 47e219b4a1..946ae32d87 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -105,13 +105,11 @@ TEST_F(FormatTestJava, ClassDeclarations) { getStyleWithColumns(60)); verifyFormat("@SomeAnnotation()\n" "abstract class aaaaaaaaaaaa\n" - " extends bbbbbbbbbbbbbbb implements cccccccccccc {\n" - "}", + " extends bbbbbbbbbbbbbbb implements cccccccccccc {}", getStyleWithColumns(76)); verifyFormat("@SomeAnnotation()\n" "abstract class aaaaaaaaa\n" - " extends bbbbbbbbbbbb implements cccccccccccc {\n" - "}", + " extends bbbbbbbbbbbb implements cccccccccccc {}", getStyleWithColumns(76)); verifyFormat("interface SomeInterface extends Foo, Bar {\n" " void doStuff(int theStuff);\n" @@ -139,31 +137,26 @@ TEST_F(FormatTestJava, EnumDeclarations) { "}"); verifyFormat("public class SomeClass {\n" " enum SomeThing { ABC, CDE }\n" - " void f() {\n" - " }\n" + " void f() {}\n" "}"); verifyFormat("public class SomeClass implements SomeInterface {\n" " enum SomeThing { ABC, CDE }\n" - " void f() {\n" - " }\n" + " void f() {}\n" "}"); verifyFormat("enum SomeThing {\n" " ABC,\n" " CDE;\n" - " void f() {\n" - " }\n" + " void f() {}\n" "}"); verifyFormat("enum SomeThing {\n" " ABC(1, \"ABC\"),\n" " CDE(2, \"CDE\");\n" - " Something(int i, String s) {\n" - " }\n" + " Something(int i, String s) {}\n" "}"); verifyFormat("enum SomeThing {\n" " ABC(new int[] {1, 2}),\n" " CDE(new int[] {2, 3});\n" - " Something(int[] i) {\n" - " }\n" + " Something(int[] i) {}\n" "}"); verifyFormat("public enum SomeThing {\n" " ABC {\n" @@ -177,8 +170,7 @@ TEST_F(FormatTestJava, EnumDeclarations) { " return \"CDE\";\n" " }\n" " };\n" - " public void f() {\n" - " }\n" + " public void f() {}\n" "}"); verifyFormat("private enum SomeEnum implements Foo {\n" " ABC {\n" @@ -205,30 +197,28 @@ TEST_F(FormatTestJava, ArrayInitializers) { TEST_F(FormatTestJava, ThrowsDeclarations) { verifyFormat("public void doSooooooooooooooooooooooooooomething()\n" - " throws LooooooooooooooooooooooooooooongException {\n}"); + " throws LooooooooooooooooooooooooooooongException {}"); } TEST_F(FormatTestJava, Annotations) { verifyFormat("@Override\n" - "public String toString() {\n}"); + "public String toString() {}"); verifyFormat("@Override\n" "@Nullable\n" - "public String getNameIfPresent() {\n}"); + "public String getNameIfPresent() {}"); verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" - "public void doSomething() {\n}"); + "public void doSomething() {}"); verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" "@Author(name = \"abc\")\n" - "public void doSomething() {\n}"); + "public void doSomething() {}"); verifyFormat("DoSomething(new A() {\n" " @Override\n" - " public String toString() {\n" - " }\n" + " public String toString() {}\n" "});"); - verifyFormat("void SomeFunction(@Nullable String something) {\n" - "}"); + verifyFormat("void SomeFunction(@Nullable String something) {}"); verifyFormat("@Partial @Mock DataLoader loader;"); verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n" @@ -246,13 +236,13 @@ TEST_F(FormatTestJava, Generics) { verifyFormat("A.doSomething();"); verifyFormat("@Override\n" - "public Map getAll() {\n}"); + "public Map getAll() {}"); - verifyFormat("public ArrayList get() {\n}"); - verifyFormat("protected ArrayList get() {\n}"); - verifyFormat("private ArrayList get() {\n}"); - verifyFormat("public static ArrayList get() {\n}"); - verifyFormat("public final Foo foo() {\n}"); + verifyFormat("public ArrayList get() {}"); + verifyFormat("protected ArrayList get() {}"); + verifyFormat("private ArrayList get() {}"); + verifyFormat("public static ArrayList get() {}"); + verifyFormat("public final Foo foo() {}"); verifyFormat("public abstract Foo foo();"); verifyFormat(" T getInstance(Class type);"); verifyFormat("Function function;"); @@ -312,12 +302,10 @@ TEST_F(FormatTestJava, ImportDeclarations) { TEST_F(FormatTestJava, MethodDeclarations) { verifyFormat("void methodName(Object arg1,\n" - " Object arg2, Object arg3) {\n" - "}", + " Object arg2, Object arg3) {}", getStyleWithColumns(40)); verifyFormat("void methodName(\n" - " Object arg1, Object arg2) {\n" - "}", + " Object arg1, Object arg2) {}", getStyleWithColumns(40)); } -- 2.40.0