Addressed comments.
Hi djasper,
http://llvm-reviews.chandlerc.com/D2372
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D2372?vs=6008&id=6018#toc
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
lib/Format/UnwrappedLineParser.h
unittests/Format/FormatTest.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -218,7 +218,11 @@
/// Like \c Attach, but break before function definitions.
BS_Stroustrup,
/// Always break before braces.
- BS_Allman
+ BS_Allman,
+ /// Always break before braces and add an extra level of indentation to
+ /// braces of control statements, not to those of class, function
+ /// or other definitions.
+ BS_GNU
};
/// \brief The brace breaking style to use.
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -68,6 +68,7 @@
IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
+ IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
}
};
@@ -359,7 +360,7 @@
FormatStyle getGNUStyle() {
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBinaryOperators = true;
- Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+ Style.BreakBeforeBraces = FormatStyle::BS_GNU;
Style.BreakBeforeTernaryOperators = true;
Style.ColumnLimit = 79;
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
@@ -561,7 +562,8 @@
SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
if (Limit == 0)
return 0;
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
+ if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+ Style.BreakBeforeBraces == FormatStyle::BS_GNU) &&
I[1]->First->is(tok::l_brace))
return 0;
if (I[1]->InPPDirective != (*I)->InPPDirective ||
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1410,7 +1410,8 @@
Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
return true;
} else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
- return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
+ return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+ Style.BreakBeforeBraces == FormatStyle::BS_GNU;
}
return false;
}
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -677,9 +677,7 @@
// structural element.
// 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_Allman)
+ if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
addUnwrappedLine();
FormatTok->Type = TT_FunctionLBrace;
parseBlock(/*MustBeDeclaration=*/false);
@@ -931,20 +929,32 @@
} while (!eof());
}
+void UnwrappedLineParser::formatControlStatementOpenBrace() {
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
+ addUnwrappedLine();
+ } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
+ addUnwrappedLine();
+ ++Line->Level;
+ }
+}
+
void UnwrappedLineParser::parseIfThenElse() {
assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
nextToken();
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
bool NeedsUnwrappedLine = false;
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
addUnwrappedLine();
- else
+ } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
+ addUnwrappedLine();
+ --Line->Level;
+ } else {
NeedsUnwrappedLine = true;
+ }
} else {
addUnwrappedLine();
++Line->Level;
@@ -954,10 +964,11 @@
if (FormatTok->Tok.is(tok::kw_else)) {
nextToken();
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
addUnwrappedLine();
+ if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+ --Line->Level;
} else if (FormatTok->Tok.is(tok::kw_if)) {
parseIfThenElse();
} else {
@@ -978,7 +989,8 @@
nextToken();
if (FormatTok->Tok.is(tok::l_brace)) {
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
- Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+ Style.BreakBeforeBraces == FormatStyle::BS_GNU)
addUnwrappedLine();
bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
@@ -1001,10 +1013,11 @@
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
addUnwrappedLine();
+ if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+ --Line->Level;
} else {
addUnwrappedLine();
++Line->Level;
@@ -1016,23 +1029,27 @@
void UnwrappedLineParser::parseDoWhile() {
assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
nextToken();
+ unsigned OldLineLevel = Line->Level;
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
} else {
addUnwrappedLine();
++Line->Level;
parseStructuralElement();
- --Line->Level;
}
// FIXME: Add error handling.
if (!FormatTok->Tok.is(tok::kw_while)) {
+ Line->Level = OldLineLevel;
addUnwrappedLine();
return;
}
+ if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+ addUnwrappedLine();
+
+ Line->Level = OldLineLevel;
nextToken();
parseStructuralElement();
}
@@ -1043,13 +1060,14 @@
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();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
if (FormatTok->Tok.is(tok::kw_break)) {
- // "break;" after "}" on its own line only for BS_Allman
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ // "break;" after "}" on its own line only for BS_Allman and BS_GNU
+ if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+ Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
addUnwrappedLine();
+ }
parseStructuralElement();
}
}
@@ -1072,10 +1090,11 @@
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
- if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
- addUnwrappedLine();
+ formatControlStatementOpenBrace();
parseBlock(/*MustBeDeclaration=*/false);
addUnwrappedLine();
+ if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+ --Line->Level;
} else {
addUnwrappedLine();
++Line->Level;
@@ -1164,7 +1183,8 @@
}
if (FormatTok->Tok.is(tok::l_brace)) {
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
- Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+ Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+ Style.BreakBeforeBraces == FormatStyle::BS_GNU)
addUnwrappedLine();
parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
Index: lib/Format/UnwrappedLineParser.h
===================================================================
--- lib/Format/UnwrappedLineParser.h
+++ lib/Format/UnwrappedLineParser.h
@@ -108,6 +108,7 @@
void pushToken(FormatToken *Tok);
void calculateBraceTypes();
void pushPPConditional();
+ void formatControlStatementOpenBrace();
// FIXME: We are constantly running into bugs where Line.Level is incorrectly
// subtracted from beyond 0. Introduce a method to subtract from Line.Level
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -6935,6 +6935,91 @@
BreakBeforeBraceShortIfs);
}
+TEST_F(FormatTest, GNUBraceBreaking) {
+ FormatStyle GNUBraceStyle = getLLVMStyle();
+ GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
+ verifyFormat("namespace a\n"
+ "{\n"
+ "class A\n"
+ "{\n"
+ " void f()\n"
+ " {\n"
+ " int a;\n"
+ " {\n"
+ " int b;\n"
+ " }\n"
+ " if (true)\n"
+ " {\n"
+ " a();\n"
+ " b();\n"
+ " }\n"
+ " }\n"
+ " void g() { return; }\n"
+ "}\n"
+ "}",
+ GNUBraceStyle);
+
+ verifyFormat("void f()\n"
+ "{\n"
+ " if (true)\n"
+ " {\n"
+ " a();\n"
+ " }\n"
+ " else if (false)\n"
+ " {\n"
+ " b();\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " c();\n"
+ " }\n"
+ "}\n",
+ GNUBraceStyle);
+
+ verifyFormat("void f()\n"
+ "{\n"
+ " for (int i = 0; i < 10; ++i)\n"
+ " {\n"
+ " a();\n"
+ " }\n"
+ " while (false)\n"
+ " {\n"
+ " b();\n"
+ " }\n"
+ " do\n"
+ " {\n"
+ " c();\n"
+ " }\n"
+ " while (false);\n"
+ "}\n",
+ GNUBraceStyle);
+
+ verifyFormat("void f(int a)\n"
+ "{\n"
+ " switch (a)\n"
+ " {\n"
+ " case 0:\n"
+ " break;\n"
+ " case 1:\n"
+ " {\n"
+ " break;\n"
+ " }\n"
+ " case 2:\n"
+ " {\n"
+ " }\n"
+ " break;\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}\n",
+ GNUBraceStyle);
+
+ verifyFormat("enum X\n"
+ "{\n"
+ " Y = 0,\n"
+ "}\n",
+ GNUBraceStyle);
+}
TEST_F(FormatTest, CatchExceptionReferenceBinding) {
verifyFormat("void f() {\n"
" try {\n"
@@ -7132,6 +7217,9 @@
FormatStyle::BS_Linux);
CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
FormatStyle::BS_Stroustrup);
+ CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
+ FormatStyle::BS_Allman);
+ CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
Style.NamespaceIndentation = FormatStyle::NI_All;
CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits