[clang] [clang-format] Add anonymous namespace indentation option (PR #244933)
Arnab Sarkar via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 28 21:23:22 PST 2026
https://github.com/28arnab updated https://github.com/llvm/llvm-project/pull/244933
>From 19453b2d9b2408e0fdd91a497cc88e569f41c883 Mon Sep 17 00:00:00 2001
From: Arnab Sarkar <arnab at 28arnab.com>
Date: Sun, 22 Nov 2026 02:07:40 +0530
Subject: [PATCH 1/2] [clang-format] Add anonymous namespace indentation option
Add IndentAnonymousNamespaces to control whether clang-format indents the contents of anonymous namespaces.
When disabled, anonymous namespaces do not add an indentation level, while named namespaces continue to follow NamespaceIndentation.
Fixes #221584
Signed-off-by: Arnab Sarkar <arnab at 28arnab.com>
---
clang/include/clang/Format/Format.h | 4 +++
clang/lib/Format/Format.cpp | 3 ++
clang/lib/Format/UnwrappedLineParser.cpp | 13 +++++++--
clang/unittests/Format/ConfigParseTest.cpp | 6 ++++
clang/unittests/Format/FormatTest.cpp | 33 ++++++++++++++++++++++
5 files changed, 56 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 1f29dd630b8020..5d2a2cdf34d873 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4064,6 +4064,9 @@ struct FormatStyle {
NI_All
};
+ /// The indentation used for anonymous namespaces.
+ bool IndentAnonymousNamespaces;
+
/// The indentation used for namespaces.
/// \version 3.7
NamespaceIndentationKind NamespaceIndentation;
@@ -6307,6 +6310,7 @@ struct FormatStyle {
MacrosSkippedByRemoveParentheses ==
R.MacrosSkippedByRemoveParentheses &&
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
+ IndentAnonymousNamespaces == R.IndentAnonymousNamespaces &&
NamespaceIndentation == R.NamespaceIndentation &&
NamespaceMacros == R.NamespaceMacros &&
NumericLiteralCase == R.NumericLiteralCase &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d284948253d628..ee893e7d5b803c 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1444,6 +1444,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.MacrosSkippedByRemoveParentheses);
IO.mapOptional("MainIncludeChar", Style.IncludeStyle.MainIncludeChar);
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
+ IO.mapOptional("IndentAnonymousNamespaces",
+ Style.IndentAnonymousNamespaces);
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
IO.mapOptional("NumericLiteralCase", Style.NumericLiteralCase);
@@ -2021,6 +2023,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.Language = Language;
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
LLVMStyle.MaxEmptyLinesToKeep = 1;
+ LLVMStyle.IndentAnonymousNamespaces = true;
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.NumericLiteralCase = {/*ExponentLetter=*/FormatStyle::NLCS_Leave,
/*HexDigit=*/FormatStyle::NLCS_Leave,
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index d4910090516463..d8750bd2999c83 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3352,6 +3352,7 @@ void UnwrappedLineParser::parseNamespace() {
"'namespace' expected");
const FormatToken &InitialToken = *FormatTok;
+ bool IsAnonymousNamespace = InitialToken.is(tok::kw_namespace);
nextToken();
if (InitialToken.is(TT_NamespaceMacro)) {
parseParens();
@@ -3359,12 +3360,15 @@ void UnwrappedLineParser::parseNamespace() {
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
tok::l_square, tok::period, tok::l_paren) ||
(Style.isCSharp() && FormatTok->is(tok::kw_union))) {
- if (FormatTok->is(tok::l_square))
+ if (FormatTok->is(tok::l_square)) {
parseSquare();
- else if (FormatTok->is(tok::l_paren))
+ } else if (FormatTok->is(tok::l_paren)) {
parseParens();
- else
+ } else {
+ if (FormatTok->is(tok::identifier))
+ IsAnonymousNamespace = false;
nextToken();
+ }
}
}
if (FormatTok->is(tok::l_brace)) {
@@ -3381,6 +3385,9 @@ void UnwrappedLineParser::parseNamespace() {
DeclarationScopeStack.size() > 1)
? 1u
: 0u;
+
+ if (IsAnonymousNamespace && !Style.IndentAnonymousNamespaces)
+ AddLevels = 0;
parseNamespaceOrExportBlock(AddLevels);
}
// FIXME: Add error handling.
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index c7903d3cc51879..1d5623a7fe0b87 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1009,6 +1009,12 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
FormatStyle::NI_All);
+ Style.IndentAnonymousNamespaces = true;
+ CHECK_PARSE("IndentAnonymousNamespaces: false", IndentAnonymousNamespaces,
+ false);
+ CHECK_PARSE("IndentAnonymousNamespaces: true", IndentAnonymousNamespaces,
+ true);
+
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index be4936e9a8d95e..dd3458f8eaf368 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "clang/Format/Format.h"
#include "FormatTestBase.h"
#define DEBUG_TYPE "format-test"
@@ -4505,6 +4506,38 @@ TEST_F(FormatTest, FormatsNamespaces) {
"} // namespace out",
Style);
+ FormatStyle AnonymousNamespace = getLLVMStyle();
+ AnonymousNamespace.NamespaceIndentation = FormatStyle::NI_All;
+ AnonymousNamespace.IndentAnonymousNamespaces = false;
+ verifyFormat("namespace A {\n"
+ " int A;\n"
+ "}\n"
+ "namespace {\n"
+ "int A;\n"
+ "}",
+ "namespace A {\n"
+ "int A;\n"
+ "}\n"
+ "namespace {\n"
+ "int A;\n"
+ "}",
+ AnonymousNamespace);
+
+ AnonymousNamespace.IndentAnonymousNamespaces = true;
+ verifyFormat("namespace A {\n"
+ " int A;\n"
+ "}\n"
+ "namespace {\n"
+ " int A;\n"
+ "}",
+ "namespace A {\n"
+ "int A;\n"
+ "}\n"
+ "namespace {\n"
+ "int A;\n"
+ "}",
+ AnonymousNamespace);
+
FormatStyle ShortInlineFunctions = getLLVMStyle();
ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
>From 33dd7cd053e024f86dc95d886ef18294eba9d1c3 Mon Sep 17 00:00:00 2001
From: Arnab Sarkar <arnab at 28arnab.com>
Date: Fri, 27 Nov 2026 03:52:45 +0530
Subject: [PATCH 2/2] [clang-format] Add `NonAnonymous` option to
`NamespaceIndentation`
Add `NI_NonAnonymous` (`NonAnonymous` in `.clang-format` configuration) to
`NamespaceIndentationKind` to indent declarations inside named
namespaces while leaving anonymous namespaces unindented.
Fixes #221584
Signed-off-by: Arnab Sarkar <arnab at 28arnab.com>
---
clang/docs/ClangFormatStyleOptions.md | 12 ++++++++++
clang/include/clang/Format/Format.h | 16 +++++++++----
clang/lib/Format/Format.cpp | 4 +---
clang/lib/Format/UnwrappedLineParser.cpp | 18 +++++++--------
clang/unittests/Format/ConfigParseTest.cpp | 8 ++-----
clang/unittests/Format/FormatTest.cpp | 27 +++++++---------------
6 files changed, 42 insertions(+), 43 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.md b/clang/docs/ClangFormatStyleOptions.md
index c7de6fc25e1f3a..30f3cd62782f1b 100644
--- a/clang/docs/ClangFormatStyleOptions.md
+++ b/clang/docs/ClangFormatStyleOptions.md
@@ -5779,6 +5779,18 @@ the configuration (without a prefix: `Auto`).
}
```
+ - `NI_NonAnonymous` (in configuration: `NonAnonymous`)
+ Indent in Non-anonymous namespaces.
+
+ ```c++
+ namespace out {
+ int i;
+ namespace {
+ int i;
+ }
+ }
+ ```
+
(namespacemacros)=
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 5d2a2cdf34d873..52383f3c88590e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4061,12 +4061,19 @@ struct FormatStyle {
/// }
/// }
/// \endcode
- NI_All
+ NI_All,
+ /// Indent in Non-anonymous namespaces.
+ /// \code
+ /// namespace out {
+ /// int i;
+ /// namespace {
+ /// int i;
+ /// }
+ /// }
+ /// \endcode
+ NI_NonAnonymous,
};
- /// The indentation used for anonymous namespaces.
- bool IndentAnonymousNamespaces;
-
/// The indentation used for namespaces.
/// \version 3.7
NamespaceIndentationKind NamespaceIndentation;
@@ -6310,7 +6317,6 @@ struct FormatStyle {
MacrosSkippedByRemoveParentheses ==
R.MacrosSkippedByRemoveParentheses &&
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
- IndentAnonymousNamespaces == R.IndentAnonymousNamespaces &&
NamespaceIndentation == R.NamespaceIndentation &&
NamespaceMacros == R.NamespaceMacros &&
NumericLiteralCase == R.NumericLiteralCase &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ee893e7d5b803c..986994c358dc97 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -601,6 +601,7 @@ struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
IO.enumCase(Value, "None", FormatStyle::NI_None);
IO.enumCase(Value, "Inner", FormatStyle::NI_Inner);
IO.enumCase(Value, "All", FormatStyle::NI_All);
+ IO.enumCase(Value, "NonAnonymous", FormatStyle::NI_NonAnonymous);
}
};
@@ -1444,8 +1445,6 @@ template <> struct MappingTraits<FormatStyle> {
Style.MacrosSkippedByRemoveParentheses);
IO.mapOptional("MainIncludeChar", Style.IncludeStyle.MainIncludeChar);
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
- IO.mapOptional("IndentAnonymousNamespaces",
- Style.IndentAnonymousNamespaces);
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
IO.mapOptional("NumericLiteralCase", Style.NumericLiteralCase);
@@ -2023,7 +2022,6 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.Language = Language;
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
LLVMStyle.MaxEmptyLinesToKeep = 1;
- LLVMStyle.IndentAnonymousNamespaces = true;
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.NumericLiteralCase = {/*ExponentLetter=*/FormatStyle::NLCS_Leave,
/*HexDigit=*/FormatStyle::NLCS_Leave,
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index d8750bd2999c83..2254975dd697d2 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3352,23 +3352,21 @@ void UnwrappedLineParser::parseNamespace() {
"'namespace' expected");
const FormatToken &InitialToken = *FormatTok;
- bool IsAnonymousNamespace = InitialToken.is(tok::kw_namespace);
+ bool IsAnonymousNamespace = false;
nextToken();
if (InitialToken.is(TT_NamespaceMacro)) {
parseParens();
} else {
+ IsAnonymousNamespace = FormatTok->is(tok::l_brace);
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
tok::l_square, tok::period, tok::l_paren) ||
(Style.isCSharp() && FormatTok->is(tok::kw_union))) {
- if (FormatTok->is(tok::l_square)) {
+ if (FormatTok->is(tok::l_square))
parseSquare();
- } else if (FormatTok->is(tok::l_paren)) {
+ else if (FormatTok->is(tok::l_paren))
parseParens();
- } else {
- if (FormatTok->is(tok::identifier))
- IsAnonymousNamespace = false;
+ else
nextToken();
- }
}
}
if (FormatTok->is(tok::l_brace)) {
@@ -3382,12 +3380,12 @@ void UnwrappedLineParser::parseNamespace() {
unsigned AddLevels =
Style.NamespaceIndentation == FormatStyle::NI_All ||
(Style.NamespaceIndentation == FormatStyle::NI_Inner &&
- DeclarationScopeStack.size() > 1)
+ DeclarationScopeStack.size() > 1) ||
+ (Style.NamespaceIndentation == FormatStyle::NI_NonAnonymous &&
+ !IsAnonymousNamespace)
? 1u
: 0u;
- if (IsAnonymousNamespace && !Style.IndentAnonymousNamespaces)
- AddLevels = 0;
parseNamespaceOrExportBlock(AddLevels);
}
// FIXME: Add error handling.
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 1d5623a7fe0b87..98c8b9241dd173 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1008,12 +1008,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::NI_Inner);
CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
FormatStyle::NI_All);
-
- Style.IndentAnonymousNamespaces = true;
- CHECK_PARSE("IndentAnonymousNamespaces: false", IndentAnonymousNamespaces,
- false);
- CHECK_PARSE("IndentAnonymousNamespaces: true", IndentAnonymousNamespaces,
- true);
+ CHECK_PARSE("NamespaceIndentation: NonAnonymous", NamespaceIndentation,
+ FormatStyle::NI_NonAnonymous);
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index dd3458f8eaf368..9da61fe623ffd0 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/Format/Format.h"
#include "FormatTestBase.h"
#define DEBUG_TYPE "format-test"
@@ -4506,37 +4505,27 @@ TEST_F(FormatTest, FormatsNamespaces) {
"} // namespace out",
Style);
- FormatStyle AnonymousNamespace = getLLVMStyle();
- AnonymousNamespace.NamespaceIndentation = FormatStyle::NI_All;
- AnonymousNamespace.IndentAnonymousNamespaces = false;
+ Style.NamespaceIndentation = FormatStyle::NI_NonAnonymous;
verifyFormat("namespace A {\n"
" int A;\n"
"}\n"
"namespace {\n"
"int A;\n"
"}",
- "namespace A {\n"
- "int A;\n"
- "}\n"
- "namespace {\n"
- "int A;\n"
- "}",
- AnonymousNamespace);
+ Style);
- AnonymousNamespace.IndentAnonymousNamespaces = true;
+ Style.FixNamespaceComments = false;
verifyFormat("namespace A {\n"
+ " namespace {\n"
" int A;\n"
+ " }\n"
"}\n"
"namespace {\n"
- " int A;\n"
- "}",
- "namespace A {\n"
- "int A;\n"
+ "namespace B {\n"
+ " int B;\n"
"}\n"
- "namespace {\n"
- "int A;\n"
"}",
- AnonymousNamespace);
+ Style);
FormatStyle ShortInlineFunctions = getLLVMStyle();
ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
More information about the cfe-commits
mailing list