aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTest.cpp
diff options
context:
space:
mode:
authorNAKAMURA Takumi <geek4civic@gmail.com>2025-01-09 17:50:40 +0900
committerNAKAMURA Takumi <geek4civic@gmail.com>2025-01-09 17:50:40 +0900
commitfea7da1b00cc97d742faede2df96c7d327950f49 (patch)
tree4de1d6b4ddc69f4f32daabb11ad5c71ab0cf895e /clang/unittests/Format/FormatTest.cpp
parent9b99dde0d47102625d93c5d1cbbc04951025a6c9 (diff)
parent0aa930a41f2d1ebf1fa90ec42da8f96d15a4dcbb (diff)
downloadllvm-users/chapuni/cov/single/nextcount.zip
llvm-users/chapuni/cov/single/nextcount.tar.gz
llvm-users/chapuni/cov/single/nextcount.tar.bz2
Merge branch 'users/chapuni/cov/single/nextcount-base' into users/chapuni/cov/single/nextcountusers/chapuni/cov/single/nextcount
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
-rw-r--r--clang/unittests/Format/FormatTest.cpp248
1 files changed, 248 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 47465a1..4d48bca 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4476,6 +4476,7 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
"int k; } // namespace out",
Style);
+ Style.ColumnLimit = 41;
verifyFormat("namespace A { namespace B { namespace C {\n"
"}}} // namespace A::B::C",
"namespace A { namespace B {\n"
@@ -4504,6 +4505,12 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
"} // namespace bbbbbb\n"
"} // namespace aaaaaa",
Style);
+
+ verifyFormat("namespace a { namespace b {\n"
+ "namespace c {\n"
+ "}}} // namespace a::b::c",
+ Style);
+
Style.ColumnLimit = 80;
// Extra semicolon after 'inner' closing brace prevents merging
@@ -5880,6 +5887,11 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
"<< SomeThing;");
+ verifyFormat("GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)(foo);",
+ "GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)\n"
+ "(foo);",
+ getLLVMStyleWithColumns(60));
+
verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
"(n, buffers))",
getChromiumStyle(FormatStyle::LK_Cpp));
@@ -28314,6 +28326,242 @@ TEST_F(FormatTest, KeepFormFeed) {
Style);
}
+TEST_F(FormatTest, ShortNamespacesOption) {
+ auto Style = getLLVMStyle();
+ Style.AllowShortNamespacesOnASingleLine = true;
+ Style.CompactNamespaces = true;
+ Style.FixNamespaceComments = false;
+
+ // Basic functionality.
+ verifyFormat("namespace foo { class bar; }", Style);
+ verifyFormat("namespace foo::bar { class baz; }", Style);
+ verifyFormat("namespace { class bar; }", Style);
+ verifyFormat("namespace foo {\n"
+ "class bar;\n"
+ "class baz;\n"
+ "}",
+ Style);
+
+ // Trailing comments prevent merging.
+ verifyFormat("namespace foo { namespace baz {\n"
+ "class qux;\n"
+ "} // comment\n"
+ "}",
+ Style);
+
+ // Make sure code doesn't walk too far on unbalanced code.
+ verifyFormat("namespace foo {", Style);
+ verifyFormat("namespace foo {\n"
+ "class baz;",
+ Style);
+ verifyFormat("namespace foo {\n"
+ "namespace bar { class baz; }",
+ Style);
+
+ // Nested namespaces.
+ verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
+
+ // Without CompactNamespaces, we won't merge consecutive namespace
+ // declarations.
+ Style.CompactNamespaces = false;
+ verifyFormat("namespace foo {\n"
+ "namespace bar { class baz; }\n"
+ "}",
+ Style);
+
+ verifyFormat("namespace foo {\n"
+ "namespace bar { class baz; }\n"
+ "namespace qux { class quux; }\n"
+ "}",
+ Style);
+
+ Style.CompactNamespaces = true;
+
+ // Varying inner content.
+ verifyFormat("namespace foo {\n"
+ "int f() { return 5; }\n"
+ "}",
+ Style);
+ verifyFormat("namespace foo { template <T> struct bar; }", Style);
+ verifyFormat("namespace foo { constexpr int num = 42; }", Style);
+
+ // Validate nested namespace wrapping scenarios around the ColumnLimit.
+ Style.ColumnLimit = 64;
+
+ // Validate just under the ColumnLimit.
+ verifyFormat(
+ "namespace foo { namespace bar { namespace baz { class qux; } } }",
+ Style);
+
+ // Validate just over the ColumnLimit.
+ verifyFormat("namespace foo { namespace baar { namespace baaz {\n"
+ "class quux;\n"
+ "}}}",
+ Style);
+
+ verifyFormat(
+ "namespace foo { namespace bar { namespace baz { namespace qux {\n"
+ "class quux;\n"
+ "}}}}",
+ Style);
+
+ // Validate that the ColumnLimit logic accounts for trailing content as well.
+ verifyFormat("namespace foo { namespace bar { class qux; } } // extra",
+ Style);
+
+ verifyFormat("namespace foo { namespace bar { namespace baz {\n"
+ "class qux;\n"
+ "}}} // extra",
+ Style);
+
+ // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing
+ // namespace comment from 'FixNamespaceComments', as it's not really necessary
+ // in this scenario, but the two options work at very different layers of the
+ // formatter, so I'm not sure how to make them interact.
+ //
+ // As it stands, the trailing comment will be added and likely make the line
+ // too long to fit within the ColumnLimit, reducing the how likely the line
+ // will still fit on a single line. The recommendation for now is to use the
+ // concatenated namespace syntax instead. e.g. 'namespace foo::bar'
+ Style.FixNamespaceComments = true;
+ verifyFormat(
+ "namespace foo { namespace bar { namespace baz {\n"
+ "class qux;\n"
+ "}}} // namespace foo::bar::baz",
+ "namespace foo { namespace bar { namespace baz { class qux; } } }",
+ Style);
+}
+
+TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
+ auto Style = getLLVMStyle();
+ Style.FixNamespaceComments = false;
+ Style.MaxEmptyLinesToKeep = 2;
+ Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;
+
+ // Empty namespace.
+ verifyFormat("namespace N {}", Style);
+
+ // Single namespace.
+ verifyFormat("namespace N {\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "}",
+ "namespace N {\n"
+ "\n"
+ "\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "\n"
+ "\n"
+ "}",
+ Style);
+
+ // Nested namespace.
+ verifyFormat("namespace N1 {\n"
+ "namespace N2 {\n"
+ "int a = 1;\n"
+ "}\n"
+ "}",
+ "namespace N1 {\n"
+ "\n"
+ "\n"
+ "namespace N2 {\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "}\n"
+ "\n"
+ "\n"
+ "}",
+ Style);
+
+ Style.CompactNamespaces = true;
+
+ verifyFormat("namespace N1 { namespace N2 {\n"
+ "int a = 1;\n"
+ "}}",
+ "namespace N1 { namespace N2 {\n"
+ "\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "\n"
+ "}}",
+ Style);
+}
+
+TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
+ auto Style = getLLVMStyle();
+ Style.FixNamespaceComments = false;
+ Style.MaxEmptyLinesToKeep = 2;
+ Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always;
+
+ // Empty namespace.
+ verifyFormat("namespace N {}", Style);
+
+ // Single namespace.
+ verifyFormat("namespace N {\n"
+ "\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "\n"
+ "}",
+ "namespace N {\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "}",
+ Style);
+
+ // Nested namespace.
+ verifyFormat("namespace N1 {\n"
+ "namespace N2 {\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "}\n"
+ "}",
+ "namespace N1 {\n"
+ "namespace N2 {\n"
+ "int a = 1;\n"
+ "}\n"
+ "}",
+ Style);
+
+ verifyFormat("namespace N1 {\n"
+ "\n"
+ "namespace N2 {\n"
+ "\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "\n"
+ "}\n"
+ "\n"
+ "}",
+ "namespace N1 {\n"
+ "\n"
+ "namespace N2 {\n"
+ "\n"
+ "\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "\n"
+ "\n"
+ "}\n"
+ "\n"
+ "}",
+ Style);
+
+ Style.CompactNamespaces = true;
+
+ verifyFormat("namespace N1 { namespace N2 {\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "}}",
+ "namespace N1 { namespace N2 {\n"
+ "int a = 1;\n"
+ "}}",
+ Style);
+}
+
} // namespace
} // namespace test
} // namespace format