summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2014-04-08 12:46:38 +0000
committerDaniel Jasper <djasper@google.com>2014-04-08 12:46:38 +0000
commitd74cf40386ac321fca0aed6404647667d2ef00f7 (patch)
tree34c11655e5f4982bc936312ef7122a1a81e80f8e /clang
parent9eb8c92b526b13491565585b0d33159d624bf811 (diff)
downloadbcm5719-llvm-d74cf40386ac321fca0aed6404647667d2ef00f7.tar.gz
bcm5719-llvm-d74cf40386ac321fca0aed6404647667d2ef00f7.zip
clang-format: Extend AllowShortFunctions.. to only merge inline functions.
Before AllowShortFunctionsOnASingleLine could either be true, merging all functions, or false, merging no functions. This patch adds a third value "Inline", which can be used to only merge short functions defined inline in a class, i.e.: void f() { return 42; } class C { void f() { return 42; } }; llvm-svn: 205760
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/Format/Format.h17
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp2
-rw-r--r--clang/lib/Format/Format.cpp28
-rw-r--r--clang/unittests/Format/FormatTest.cpp35
4 files changed, 67 insertions, 15 deletions
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index cded911edbf..a671845b45f 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -163,9 +163,20 @@ struct FormatStyle {
/// single line.
bool AllowShortLoopsOnASingleLine;
- /// \brief If \c true, <tt>int f() { return 0; }</tt> can be put on a single
- /// line.
- bool AllowShortFunctionsOnASingleLine;
+ /// \brief Different styles for merging short functions containing at most one
+ /// statement.
+ enum ShortFunctionStyle {
+ /// \brief Never merge functions into a single line.
+ SFS_None,
+ /// \brief Only merge functions defined inside a class.
+ SFS_Inline,
+ /// \brief Merge all functions fitting on a single line.
+ SFS_All,
+ };
+
+ /// \brief Dependent on the value, <tt>int f() { return 0; }</tt> can be put
+ /// on a single line.
+ ShortFunctionStyle AllowShortFunctionsOnASingleLine;
/// \brief Add a space after \c @property in Objective-C, i.e. use
/// <tt>@property (readonly)</tt> instead of <tt>@property(readonly)</tt>.
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 5a3dee63870..5317fb33ffb 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -145,7 +145,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
return true;
if (Current.Type == TT_CtorInitializerColon &&
- (!Style.AllowShortFunctionsOnASingleLine ||
+ ((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) ||
Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
return true;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 44116a8f8e3..5367106535d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -65,6 +65,16 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
}
};
+template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
+ static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
+ IO.enumCase(Value, "None", FormatStyle::SFS_None);
+ IO.enumCase(Value, "false", FormatStyle::SFS_None);
+ IO.enumCase(Value, "All", FormatStyle::SFS_All);
+ IO.enumCase(Value, "true", FormatStyle::SFS_All);
+ IO.enumCase(Value, "Inline", FormatStyle::SFS_Inline);
+ }
+};
+
template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {
static void enumeration(IO &IO, FormatStyle::BraceBreakingStyle &Value) {
IO.enumCase(Value, "Attach", FormatStyle::BS_Attach);
@@ -251,7 +261,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.AlignEscapedNewlinesLeft = false;
LLVMStyle.AlignTrailingComments = true;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
- LLVMStyle.AllowShortFunctionsOnASingleLine = true;
+ LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
LLVMStyle.AllowShortLoopsOnASingleLine = false;
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
@@ -332,7 +342,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.MaxEmptyLinesToKeep = 2;
GoogleStyle.SpacesInContainerLiterals = false;
} else if (Language == FormatStyle::LK_Proto) {
- GoogleStyle.AllowShortFunctionsOnASingleLine = false;
+ GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
}
return GoogleStyle;
@@ -341,6 +351,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
FormatStyle ChromiumStyle = getGoogleStyle(Language);
ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
+ ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
ChromiumStyle.BinPackParameters = false;
@@ -523,11 +534,16 @@ public:
if (I + 1 == E || I[1]->Type == LT_Invalid)
return 0;
+ // FIXME: TheLine->Level != 0 might or might not be the right check to do.
+ // If necessary, change to something smarter.
+ bool MergeShortFunctions =
+ Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
+ (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline &&
+ TheLine->Level != 0);
+
if (TheLine->Last->Type == TT_FunctionLBrace &&
TheLine->First != TheLine->Last) {
- return Style.AllowShortFunctionsOnASingleLine
- ? tryMergeSimpleBlock(I, E, Limit)
- : 0;
+ return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
}
if (TheLine->Last->is(tok::l_brace)) {
return Style.BreakBeforeBraces == FormatStyle::BS_Attach
@@ -542,7 +558,7 @@ public:
Limit -= 2;
unsigned MergedLines = 0;
- if (Style.AllowShortFunctionsOnASingleLine) {
+ if (MergeShortFunctions) {
MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
// If we managed to merge the block, count the function header, which is
// on a separate line.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 90e6ff72037..d59ed6c0aff 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5162,7 +5162,7 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
FormatStyle DoNotMerge = getLLVMStyle();
- DoNotMerge.AllowShortFunctionsOnASingleLine = false;
+ DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
verifyFormat("void f() { return 42; }");
verifyFormat("void f() {\n"
@@ -5219,7 +5219,8 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
format("A()\n:b(0)\n{\n}", NoColumnLimit));
FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
- DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = false;
+ DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
+ FormatStyle::SFS_None;
EXPECT_EQ("A()\n"
" : b(0) {\n"
"}",
@@ -5249,6 +5250,19 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
getLLVMStyleWithColumns(23));
}
+TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
+ FormatStyle MergeInlineOnly = getLLVMStyle();
+ MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
+ verifyFormat("class C {\n"
+ " int f() { return 42; }\n"
+ "};",
+ MergeInlineOnly);
+ verifyFormat("int f() {\n"
+ " return 42;\n"
+ "}",
+ MergeInlineOnly);
+}
+
TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
// Elaborate type variable declarations.
verifyFormat("struct foo a = {bar};\nint n;");
@@ -7537,7 +7551,6 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
CHECK_PARSE_BOOL(AlignTrailingComments);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
- CHECK_PARSE_BOOL(AllowShortFunctionsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
@@ -7590,6 +7603,18 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
+ CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
+ AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
+
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
FormatStyle::SBPO_Never);
@@ -7943,7 +7968,7 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
"}",
Style);
- Style.AllowShortFunctionsOnASingleLine = false;
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
verifyFormat("SomeClass::Constructor()\n"
" : a(a)\n"
" , b(b)\n"
@@ -7954,7 +7979,7 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
Style);
Style.ColumnLimit = 80;
- Style.AllowShortFunctionsOnASingleLine = true;
+ Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Style.ConstructorInitializerIndentWidth = 2;
verifyFormat("SomeClass::Constructor()\n"
" : a(a)\n"
OpenPOWER on IntegriCloud