[PATCH] Added the Allman brace style
Joe Hermaszewski
expipiplus1 at gmail.com
Tue May 28 07:20:14 PDT 2013
Hi djasper,
A brace style where breaks are inserted before every opening brace of a compound statement and the brace indented to be in line with the enclosing scope.
http://llvm-reviews.chandlerc.com/D880
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -112,7 +112,9 @@
/// class definitions.
BS_Linux,
/// Like \c Attach, but break before function definitions.
- BS_Stroustrup
+ BS_Stroustrup,
+ /// Break before all compound statements
+ BS_Allman
};
/// \brief The brace breaking style to use.
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -50,6 +50,7 @@
IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
+ IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
}
};
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -207,6 +207,8 @@
addUnwrappedLine();
break;
case tok::l_brace:
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
// FIXME: Add parameter whether this can happen - if this happens, we must
// be in a non-declaration context.
parseBlock(/*MustBeDeclaration=*/ false);
@@ -520,6 +522,8 @@
if (FormatTok.Tok.is(tok::string_literal)) {
nextToken();
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ true, 0);
addUnwrappedLine();
return;
@@ -564,7 +568,8 @@
// FIXME: Figure out cases where this is not true, and add projections
// for them (the one we know is missing are lambdas).
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
- Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
+ Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman)
addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
addUnwrappedLine();
@@ -725,6 +730,8 @@
parseParens();
bool NeedsUnwrappedLine = false;
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
NeedsUnwrappedLine = true;
} else {
@@ -734,8 +741,13 @@
--Line->Level;
}
if (FormatTok.Tok.is(tok::kw_else)) {
+ // Don't cuddle the else
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
nextToken();
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
addUnwrappedLine();
} else if (FormatTok.Tok.is(tok::kw_if)) {
@@ -757,7 +769,8 @@
if (FormatTok.Tok.is(tok::identifier))
nextToken();
if (FormatTok.Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman)
addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ true, 0);
@@ -777,6 +790,8 @@
if (FormatTok.Tok.is(tok::l_paren))
parseParens();
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
addUnwrappedLine();
} else {
@@ -791,6 +806,8 @@
assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
nextToken();
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
} else {
addUnwrappedLine();
@@ -817,6 +834,8 @@
if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
--Line->Level;
if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false);
if (FormatTok.Tok.is(tok::kw_break))
parseStructuralElement(); // "break;" after "}" goes on the same line.
@@ -840,6 +859,8 @@
if (FormatTok.Tok.is(tok::l_paren))
parseParens();
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
addUnwrappedLine();
} else {
@@ -876,6 +897,8 @@
nextToken();
}
if (FormatTok.Tok.is(tok::l_brace)) {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ addUnwrappedLine();
nextToken();
addUnwrappedLine();
++Line->Level;
@@ -939,7 +962,8 @@
}
}
if (FormatTok.Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman)
addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/ true);
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -4561,6 +4561,30 @@
BreakBeforeBrace);
}
+TEST_F(FormatTest, AllmanBraceBreaking) {
+ FormatStyle BreakBeforeBrace = getLLVMStyle();
+ BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
+ verifyFormat("namespace a\n"
+ "{\n"
+ "class A\n"
+ "{\n"
+ " void f()\n"
+ " {\n"
+ " if (true)\n"
+ " {\n"
+ " a();\n"
+ " b();\n"
+ " }\n"
+ " }\n"
+ " void g()\n"
+ " {\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "}",
+ BreakBeforeBrace);
+}
+
bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
for (size_t i = 1; i < Styles.size(); ++i)
if (!(Styles[0] == Styles[i]))
@@ -4648,6 +4672,8 @@
FormatStyle::BS_Linux);
CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
FormatStyle::BS_Stroustrup);
+ CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
+ FormatStyle::BS_Allman);
#undef CHECK_PARSE
#undef CHECK_PARSE_BOOL
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D880.1.patch
Type: text/x-patch
Size: 6811 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130528/3e978255/attachment.bin>
More information about the cfe-commits
mailing list