Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6771,6 +6771,18 @@ the configuration (without a prefix: ``Auto``).
int a [5]; vs. int a[5];
int a [5][5]; vs. int a[5][5];

.. _SpaceBetweenUnderscoreParens:

**SpaceBetweenUnderscoreParens** (``Boolean``) :versionbadge:`clang-format 19` :ref:`¶ <SpaceBetweenUnderscoreParens>`
If ``false``, spaces will be removed between underscore and an opening
parenthesis. This is specifically for the gettext macro ``_()`` commonly
used in GNU projects.

.. code-block:: c++

true: false:
_ (message); vs. _(message);

.. _SpaceInEmptyBlock:

**SpaceInEmptyBlock** (``Boolean``) :versionbadge:`clang-format 10` :ref:`¶ <SpaceInEmptyBlock>`
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -5030,6 +5030,16 @@ struct FormatStyle {
/// \version 10
bool SpaceBeforeSquareBrackets;

/// If ``false``, spaces will be removed between underscore and an opening
/// parenthesis. This is specifically for the gettext macro ``_()`` commonly
/// used in GNU projects.
/// \code
/// true: false:
/// _ (message); vs. _(message);
/// \endcode
/// \version 19
bool SpaceBetweenUnderscoreParens;

/// If ``false``, spaces will be removed before range-based for loop
/// colon.
/// \code
Expand Down Expand Up @@ -5766,6 +5776,7 @@ struct FormatStyle {
SpaceBeforeRangeBasedForLoopColon ==
R.SpaceBeforeRangeBasedForLoopColon &&
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
SpaceBetweenUnderscoreParens == R.SpaceBetweenUnderscoreParens &&
SpaceInEmptyBraces == R.SpaceInEmptyBraces &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
SpacesInAngles == R.SpacesInAngles &&
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.SpaceBeforeRangeBasedForLoopColon);
IO.mapOptional("SpaceBeforeSquareBrackets",
Style.SpaceBeforeSquareBrackets);
IO.mapOptional("SpaceBetweenUnderscoreParens",
Style.SpaceBetweenUnderscoreParens);
IO.mapOptional("SpaceInEmptyBraces", Style.SpaceInEmptyBraces);
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
Expand Down Expand Up @@ -1821,6 +1823,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SpaceBeforeParensOptions.AfterIfMacros = true;
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
LLVMStyle.SpaceBeforeSquareBrackets = false;
LLVMStyle.SpaceBetweenUnderscoreParens = true;
LLVMStyle.SpaceInEmptyBraces = FormatStyle::SIEB_Never;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never;
Expand Down Expand Up @@ -2146,6 +2149,7 @@ FormatStyle getGNUStyle() {
Style.FixNamespaceComments = false;
Style.KeepFormFeed = true;
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Style.SpaceBetweenUnderscoreParens = false;
return Style;
}

Expand Down
13 changes: 13 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,14 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
}

bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
// Handle underscore macro: _("string") or _(message)
// Special case when SpaceBetweenUnderscoreParens is false
const FormatToken *Left = Right.Previous;
if (Left && Left->is(tok::identifier) && Left->TokenText == "_" &&
!Style.SpaceBetweenUnderscoreParens) {
return false;
}

if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
return true;
if (Right.is(TT_OverloadedOperatorLParen) &&
Expand Down Expand Up @@ -4920,6 +4928,11 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
// Handle builtins like identifiers.
if (Line.Type != LT_PreprocessorDirective &&
(Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
// Check for special case: single underscore token (gettext macro).
if (Left.Tok.getIdentifierInfo() && !Style.SpaceBetweenUnderscoreParens &&
Left.TokenText == "_") {
return false;
}
return spaceRequiredBeforeParens(Right);
}
return false;
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17877,6 +17877,37 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
Spaces);
}

TEST_F(FormatTest, SpaceBetweenUnderscoreParens) {
FormatStyle Style = getLLVMStyle();
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;

EXPECT_TRUE(Style.SpaceBetweenUnderscoreParens);
verifyFormat("func (arg);", Style);
verifyFormat("my_func (arg);", Style);
verifyFormat("_ (message);", Style);
verifyFormat("underscore_ (param);", Style);

Style.SpaceBetweenUnderscoreParens = false;
verifyFormat("func (arg);", Style);
verifyFormat("my_func (arg);", Style);
verifyFormat("_(message);", Style);
verifyFormat("underscore_ (param);", Style);
verifyFormat("_private_func (data);", Style);

FormatStyle GNUStyle = getGNUStyle();
EXPECT_FALSE(GNUStyle.SpaceBetweenUnderscoreParens);
EXPECT_EQ(GNUStyle.SpaceBeforeParens, FormatStyle::SBPO_Always);
verifyFormat("func (arg);", GNUStyle);
verifyFormat("my_func (arg);", GNUStyle);
verifyFormat("_(message);", GNUStyle);
verifyFormat("_private_func (data);", GNUStyle);

verifyFormat("printf (_(\"Hello\"));\n"
"func (arg);\n"
"_(\"World\");",
GNUStyle);
}

TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
verifyFormat("int a[5];");
verifyFormat("a[3] += 42;");
Expand Down