Skip to content

Commit

Permalink
Removes extra space between new and (std::nothrow)
Browse files Browse the repository at this point in the history
Adds SBPO_Haiku option in spaceBeforeParensOptions and use it by default for the Haiku style.
  • Loading branch information
saloniig authored and pulkomandy committed Jul 14, 2021
1 parent c5d9548 commit e7062af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
10 changes: 9 additions & 1 deletion clang/include/clang/Format/Format.h
Expand Up @@ -3123,7 +3123,15 @@ struct FormatStyle {
/// }
/// }
/// \endcode
SBPO_Always
SBPO_Always,
/// Same as ``SBPO_ControlStatements`` except this option doesn't apply to
/// new keyword.
/// \code
/// void f() {
/// new(std::nothrow)
/// }
/// \endcode
SBPO_Haiku
};

/// Defines in which cases to put a space before opening parentheses.
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Expand Up @@ -441,6 +441,7 @@ struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> {
IO.enumCase(Value, "NonEmptyParentheses",
FormatStyle::SBPO_NonEmptyParentheses);
IO.enumCase(Value, "Always", FormatStyle::SBPO_Always);
IO.enumCase(Value, "Haiku", FormatStyle::SBPO_Haiku);

// For backward compatibility.
IO.enumCase(Value, "false", FormatStyle::SBPO_Never);
Expand Down Expand Up @@ -1473,6 +1474,8 @@ Style.AccessModifierOffset = -4;
Style.UseTab = FormatStyle::UT_Always;
Style.SpaceAfterCStyleCast = false;
Style.SpaceAfterTemplateKeyword = false;
Style.SpaceBeforeParens = FormatStyle::SBPO_Haiku;

return Style;
}

Expand Down
25 changes: 24 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -3140,7 +3140,27 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
FormatStyle::SBPO_ControlStatementsExceptControlMacros &&
Left.is(TT_IfMacro))
return false;
return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||

if(Style.SpaceBeforeParens == FormatStyle::SBPO_Haiku){
return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
(Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
tok::kw_switch, tok::kw_case, TT_ForEachMacro,
TT_ObjCForIn) ||
Left.isIf(Line.Type != LT_PreprocessorDirective) ||
(Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
tok::kw_delete) &&
(!Left.Previous || Left.Previous->isNot(tok::period))))) ||
(spaceRequiredBeforeParens(Right) &&
(Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
Left.is(tok::r_paren) || Left.isSimpleTypeSpecifier() ||
(Left.is(tok::r_square) && Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare))) &&
Line.Type != LT_PreprocessorDirective);

}
else{
return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
(Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
(Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
tok::kw_switch, tok::kw_case, TT_ForEachMacro,
Expand All @@ -3155,6 +3175,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
(Left.is(tok::r_square) && Left.MatchingParen &&
Left.MatchingParen->is(TT_LambdaLSquare))) &&
Line.Type != LT_PreprocessorDirective);

}

}
if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
return false;
Expand Down

0 comments on commit e7062af

Please sign in to comment.