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 @@ -6605,6 +6605,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
10 changes: 10 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4896,6 +4896,16 @@ struct FormatStyle {
/// \version 10
// bool SpaceInEmptyBlock;

/// 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;

/// Style of when to insert a space in empty braces.
enum SpaceInEmptyBracesStyle : int8_t {
/// Always insert a space in empty braces.
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 @@ -1219,6 +1219,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.SpaceBeforeCtorInitializerColon);
IO.mapOptional("SpaceBeforeInheritanceColon",
Style.SpaceBeforeInheritanceColon);
IO.mapOptional("SpaceBetweenUnderscoreParens",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resort here (and below) also.

Style.SpaceBetweenUnderscoreParens);
IO.mapOptional("SpaceBeforeJsonColon", Style.SpaceBeforeJsonColon);
IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
IO.mapOptional("SpaceBeforeParensOptions", Style.SpaceBeforeParensOptions);
Expand Down Expand Up @@ -1713,6 +1715,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SpaceBeforeCpp11BracedList = false;
LLVMStyle.SpaceBeforeCtorInitializerColon = true;
LLVMStyle.SpaceBeforeInheritanceColon = true;
LLVMStyle.SpaceBetweenUnderscoreParens = true;
LLVMStyle.SpaceBeforeJsonColon = false;
LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
LLVMStyle.SpaceBeforeParensOptions = {};
Expand Down Expand Up @@ -2044,6 +2047,7 @@ FormatStyle getGNUStyle() {
Style.FixNamespaceComments = false;
Style.KeepFormFeed = true;
Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
Style.SpaceBetweenUnderscoreParens = false;
return Style;
}

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4901,6 +4901,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 @@ -17852,6 +17852,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