llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-format Author: Arnab Sarkar (28arnab) <details> <summary>Changes</summary> 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 --- Full diff: https://github.com/llvm/llvm-project/pull/244933.diff 5 Files Affected: - (modified) clang/include/clang/Format/Format.h (+4) - (modified) clang/lib/Format/Format.cpp (+3) - (modified) clang/lib/Format/UnwrappedLineParser.cpp (+10-3) - (modified) clang/unittests/Format/ConfigParseTest.cpp (+6) - (modified) clang/unittests/Format/FormatTest.cpp (+33) ``````````diff 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 = `````````` </details> https://github.com/llvm/llvm-project/pull/244933 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
