diff options
author | Ronald Wampler <rdwampler@gmail.com> | 2019-03-26 20:18:14 +0000 |
---|---|---|
committer | Ronald Wampler <rdwampler@gmail.com> | 2019-03-26 20:18:14 +0000 |
commit | a83e2dbb1e1904dda43869597e12f9af20e0d1c8 (patch) | |
tree | 737a9c39be5ba1c5a925358bb525756b58a185ba /clang/lib/Format | |
parent | 02e96648d75bcd04ad8b4a9e976c8ff8d1a9419e (diff) | |
download | bcm5719-llvm-a83e2dbb1e1904dda43869597e12f9af20e0d1c8.tar.gz bcm5719-llvm-a83e2dbb1e1904dda43869597e12f9af20e0d1c8.zip |
[clang-format] Add style option AllowShortLambdasOnASingleLine
Summary:
This option `AllowShortLambdasOnASingleLine` similar to the other `AllowShort*` options, but applied to C++ lambdas.
Reviewers: djasper, klimek
Reviewed By: klimek
Subscribers: MyDeveloperDay, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57687
llvm-svn: 357027
Diffstat (limited to 'clang/lib/Format')
-rw-r--r-- | clang/lib/Format/Format.cpp | 14 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.h | 1 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 25 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 1 |
4 files changed, 35 insertions, 6 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 2d1adb58e4a..39651ab74d6 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -119,6 +119,17 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ShortIfStyle> { } }; +template <> struct ScalarEnumerationTraits<FormatStyle::ShortLambdaStyle> { + static void enumeration(IO &IO, FormatStyle::ShortLambdaStyle &Value) { + IO.enumCase(Value, "None", FormatStyle::SLS_None); + IO.enumCase(Value, "false", FormatStyle::SLS_None); + IO.enumCase(Value, "Empty", FormatStyle::SLS_Empty); + IO.enumCase(Value, "Inline", FormatStyle::SLS_Inline); + IO.enumCase(Value, "All", FormatStyle::SLS_All); + IO.enumCase(Value, "true", FormatStyle::SLS_All); + } +}; + template <> struct ScalarEnumerationTraits<FormatStyle::BinPackStyle> { static void enumeration(IO &IO, FormatStyle::BinPackStyle &Value) { IO.enumCase(Value, "Auto", FormatStyle::BPS_Auto); @@ -347,6 +358,8 @@ template <> struct MappingTraits<FormatStyle> { Style.AllowShortCaseLabelsOnASingleLine); IO.mapOptional("AllowShortFunctionsOnASingleLine", Style.AllowShortFunctionsOnASingleLine); + IO.mapOptional("AllowShortLambdasOnASingleLine", + Style.AllowShortLambdasOnASingleLine); IO.mapOptional("AllowShortIfStatementsOnASingleLine", Style.AllowShortIfStatementsOnASingleLine); IO.mapOptional("AllowShortLoopsOnASingleLine", @@ -653,6 +666,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.AllowShortBlocksOnASingleLine = false; LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; + LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; LLVMStyle.AllowShortLoopsOnASingleLine = false; LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 461e0704a3f..f54ffe9d54c 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -65,6 +65,7 @@ namespace format { TYPE(JsTypeOperator) \ TYPE(JsTypeOptionalQuestion) \ TYPE(LambdaArrow) \ + TYPE(LambdaLBrace) \ TYPE(LambdaLSquare) \ TYPE(LeadingJavaAnnotation) \ TYPE(LineComment) \ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 04fc3a64f55..c7b148c7909 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1191,11 +1191,11 @@ private: // Reset token type in case we have already looked at it and then // recovered from an error (e.g. failure to find the matching >). - if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro, - TT_FunctionLBrace, TT_ImplicitStringLiteral, - TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow, - TT_OverloadedOperator, TT_RegexLiteral, - TT_TemplateString, TT_ObjCStringLiteral)) + if (!CurrentToken->isOneOf( + TT_LambdaLSquare, TT_LambdaLBrace, TT_ForEachMacro, + TT_FunctionLBrace, TT_ImplicitStringLiteral, TT_InlineASMBrace, + TT_JsFatArrow, TT_LambdaArrow, TT_OverloadedOperator, + TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral)) CurrentToken->Type = TT_Unknown; CurrentToken->Role.reset(); CurrentToken->MatchingParen = nullptr; @@ -2896,7 +2896,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. static bool isAllmanBrace(const FormatToken &Tok) { return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block && - !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral); + !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); } bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, @@ -3024,6 +3024,19 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine) return true; + if (Left.is(TT_LambdaLBrace)) { + if (Left.MatchingParen && Left.MatchingParen->Next && + Left.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren) && + Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) + return false; + + if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None || + Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline || + (!Left.Children.empty() && + Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) + return true; + } + // Put multiple C# attributes on a new line. if (Style.isCSharp() && ((Left.is(TT_AttributeSquare) && Left.is(tok::r_square)) || diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 5c7ab1240c4..010b086587e 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1475,6 +1475,7 @@ bool UnwrappedLineParser::tryToParseLambda() { return true; } } + FormatTok->Type = TT_LambdaLBrace; LSquare.Type = TT_LambdaLSquare; parseChildBlock(); return true; |