diff options
author | Daniel Jasper <djasper@google.com> | 2014-05-14 09:33:35 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2014-05-14 09:33:35 +0000 |
commit | 17605d396118f3066a0bd4dfd128b7a8893f8b94 (patch) | |
tree | 82106504d63bca0ddbbd89890960e8807e46ee0a /clang/lib/Format | |
parent | 75a58f402871d686466ce9cfd9acf1197c9e12c3 (diff) | |
download | bcm5719-llvm-17605d396118f3066a0bd4dfd128b7a8893f8b94.tar.gz bcm5719-llvm-17605d396118f3066a0bd4dfd128b7a8893f8b94.zip |
clang-format: Add option to allow short blocks on a single line.
With AllowShortBlocksOnASingleLine, clang-format allows:
if (a) { return; }
Based on patch by Gonzalo BG, thank you!
llvm-svn: 208765
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/Format.cpp | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 60706b1b3be..e0b2961af35 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -151,6 +151,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", Style.AllowAllParametersOfDeclarationOnNextLine); + IO.mapOptional("AllowShortBlocksOnASingleLine", + Style.AllowShortBlocksOnASingleLine); IO.mapOptional("AllowShortIfStatementsOnASingleLine", Style.AllowShortIfStatementsOnASingleLine); IO.mapOptional("AllowShortLoopsOnASingleLine", @@ -263,6 +265,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.AlignTrailingComments = true; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; + LLVMStyle.AllowShortBlocksOnASingleLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.AllowShortLoopsOnASingleLine = false; LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; @@ -609,7 +612,7 @@ private: return 0; if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU) && - I[1]->First->is(tok::l_brace)) + (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine)) return 0; if (I[1]->InPPDirective != (*I)->InPPDirective || (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) @@ -635,16 +638,31 @@ private: tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I, SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { - // First, check that the current line allows merging. This is the case if - // we're not in a control flow statement and the last token is an opening - // brace. AnnotatedLine &Line = **I; - if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, - tok::kw_else, tok::kw_try, tok::kw_catch, - tok::kw_for, tok::kw_case, - // This gets rid of all ObjC @ keywords and methods. - tok::at, tok::minus, tok::plus)) + + // Don't merge ObjC @ keywords and methods. + if (Line.First->isOneOf(tok::at, tok::minus, tok::plus)) + return 0; + + // Check that the current line allows merging. This depends on whether we + // are in a control flow statements as well as several style flags. + if (Line.First->isOneOf(tok::kw_else, tok::kw_case)) return 0; + if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try, + tok::kw_catch, tok::kw_for, tok::r_brace)) { + if (!Style.AllowShortBlocksOnASingleLine) + return 0; + if (!Style.AllowShortIfStatementsOnASingleLine && + Line.First->is(tok::kw_if)) + return 0; + if (!Style.AllowShortLoopsOnASingleLine && + Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for)) + return 0; + // FIXME: Consider an option to allow short exception handling clauses on + // a single line. + if (Line.First->isOneOf(tok::kw_try, tok::kw_catch)) + return 0; + } FormatToken *Tok = I[1]->First; if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && @@ -672,7 +690,8 @@ private: if (I[1]->Last->Type == TT_LineComment) return 0; do { - if (Tok->isOneOf(tok::l_brace, tok::r_brace)) + if (Tok->isOneOf(tok::l_brace, tok::r_brace) && + !Style.AllowShortBlocksOnASingleLine) return 0; Tok = Tok->Next; } while (Tok); |