From 256200732111afd03bb7437564f3a3d77c0ec3f5 Mon Sep 17 00:00:00 2001 From: rmarker <37921131+rmarker@users.noreply.github.com> Date: Mon, 5 Feb 2024 06:56:32 +1030 Subject: [clang-format] Add Automatic and ExceptShortType options for AlwaysBreakAfterReturnType. (#78011) The RTBS_None option in Clang-format avoids breaking after a short return type. However, there was an issue with the behaviour in that it wouldn't take the leading indentation of the line into account. This meant that the behaviour wasn't applying when intended. In order to address this situation without breaking the existing formatting, RTBS_None has been deprecated. In its place are two new options for AlwaysBreakAfterReturnType. The option RTBS_Automatic will break after the return type based on PenaltyReturnTypeOnItsOwnLine. The option RTBS_ExceptShortType will take the leading indentation into account and prevent breaking after short return types. This allows the inconsistent behaviour of RTBS_None to be avoided and users to decide whether they want to allow breaking after short return types or not. Resolves #78010 --- clang/lib/Format/ContinuationIndenter.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'clang/lib/Format/ContinuationIndenter.cpp') diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index a3eb913..a3aca4a 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -328,9 +328,17 @@ bool ContinuationIndenter::canBreak(const LineState &State) { // Don't break after very short return types (e.g. "void") as that is often // unexpected. - if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) { - if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) + if (Current.is(TT_FunctionDeclarationName)) { + if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None && + State.Column < 6) { return false; + } + + if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) { + assert(State.Column >= State.FirstIndent); + if (State.Column - State.FirstIndent < 6) + return false; + } } // If binary operators are moved to the next line (including commas for some @@ -587,7 +595,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { !State.Line->ReturnTypeWrapped && // Don't break before a C# function when no break after return type. (!Style.isCSharp() || - Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) && + Style.AlwaysBreakAfterReturnType > FormatStyle::RTBS_ExceptShortType) && // Don't always break between a JavaScript `function` and the function // name. !Style.isJavaScript() && Previous.isNot(tok::kw_template) && -- cgit v1.1