[clang] f955db5 - [clang-format] Add anonymous namespace indentation option (#244933)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 14:56:19 PST 2026


Author: Arnab Sarkar
Date: 2026-12-01T22:56:13Z
New Revision: f955db5481a99792d4244ecda8ecd877810a846b

URL: https://github.com/llvm/llvm-project/commit/f955db5481a99792d4244ecda8ecd877810a846b
DIFF: https://github.com/llvm/llvm-project/commit/f955db5481a99792d4244ecda8ecd877810a846b.diff

LOG: [clang-format] Add anonymous namespace indentation option (#244933)

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>

Added: 
    

Modified: 
    clang/docs/ClangFormatStyleOptions.md
    clang/docs/ReleaseNotes.md
    clang/include/clang/Format/Format.h
    clang/lib/Format/Format.cpp
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/ConfigParseTest.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ClangFormatStyleOptions.md b/clang/docs/ClangFormatStyleOptions.md
index 6d76ca6af604ea..a6a308660bad2f 100644
--- a/clang/docs/ClangFormatStyleOptions.md
+++ b/clang/docs/ClangFormatStyleOptions.md
@@ -5781,6 +5781,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/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 3c6ec715d6e699..e741c9db6969d3 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1705,6 +1705,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Recognize `.mts` and `.cts` files as TypeScript, including in
   `git-clang-format` and `clang-format-
diff .py`. The latter also now includes
   `.mjs` and `.cjs` files by default.
+- Add `NonAnonymous` option to `NamespaceIndentation` to indent declarations inside
+  named namespaces while leaving anonymous namespaces unindented.
 
 ### libclang
 

diff  --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 07847543bc7cbc..bb5f08e1e5eca9 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4063,7 +4063,17 @@ 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 namespaces.

diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d284948253d628..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);
   }
 };
 

diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 00903dcb46e111..e1fb654d1ec087 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3539,10 +3539,12 @@ void UnwrappedLineParser::parseNamespace() {
          "'namespace' expected");
 
   const FormatToken &InitialToken = *FormatTok;
+  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))) {
@@ -3565,9 +3567,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;
+
     parseNamespaceOrExportBlock(AddLevels);
   }
   // FIXME: Add error handling.

diff  --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index c7903d3cc51879..98c8b9241dd173 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1008,6 +1008,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
               FormatStyle::NI_Inner);
   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
               FormatStyle::NI_All);
+  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 b7e82e6ba2930a..478fe702fdcf5f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4505,6 +4505,28 @@ TEST_F(FormatTest, FormatsNamespaces) {
                "} // namespace out",
                Style);
 
+  Style.NamespaceIndentation = FormatStyle::NI_NonAnonymous;
+  verifyFormat("namespace A {\n"
+               "  int A;\n"
+               "}\n"
+               "namespace {\n"
+               "int A;\n"
+               "}",
+               Style);
+
+  Style.FixNamespaceComments = false;
+  verifyFormat("namespace A {\n"
+               "  namespace {\n"
+               "  int A;\n"
+               "  }\n"
+               "}\n"
+               "namespace {\n"
+               "namespace B {\n"
+               "  int B;\n"
+               "}\n"
+               "}",
+               Style);
+
   FormatStyle ShortInlineFunctions = getLLVMStyle();
   ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
   ShortInlineFunctions.AllowShortFunctionsOnASingleLine =


        


More information about the cfe-commits mailing list