[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
https://github.com/victorvianna updated https://github.com/llvm/llvm-project/pull/140888 >From 84099188f343220d48b56f164c7f541d8515f629 Mon Sep 17 00:00:00 2001 From: Victor Hugo Vianna Silva Date: Thu, 22 May 2025 10:47:09 +0100 Subject: [PATCH] ClangFormat: Insert braces & newline for Chromium This addresses an old TODO in the Chromium codebase to upstream a change to the Chromium format style. https://source.chromium.org/chromium/chromium/src/+/main:.clang-format;l=10;drc=b9d8d6aeeef9feacf6eb4838cdccca4c2da2a0eb Bug: crbug.com/40247920 --- clang/lib/Format/Format.cpp | 3 +++ clang/unittests/Format/FormatTest.cpp | 14 +++--- clang/unittests/Format/FormatTestJS.cpp | 2 +- clang/unittests/Format/FormatTestJava.cpp | 4 ++-- clang/unittests/Format/FormatTestObjC.cpp | 2 +- clang/unittests/Format/FormatTestUtils.h | 6 ++ 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0cfa061681053..389a5f0ea0b5b 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve; + ChromiumStyle.InsertBraces = true; + ChromiumStyle.InsertNewlineAtEOF = true; + if (Language == FormatStyle::LK_Java) { ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c0633ba3c29b3..2e734927e6894 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5407,7 +5407,7 @@ TEST_F(FormatTest, DoesntRemoveUnknownTokens) { } TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { - FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); + FormatStyle style = getChromiumStyle(); style.IndentWidth = 4; style.PPIndentWidth = 1; @@ -5905,7 +5905,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " "(n, buffers))", - getChromiumStyle(FormatStyle::LK_Cpp)); + getChromiumStyle()); // See PR41483 verifyNoChange("/**/ FOO(a)\n" @@ -11605,7 +11605,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("auto x = [] { A>> a; };", "auto x=[]{A >> a;};", getGoogleStyle()); - verifyFormat("A> a;", getChromiumStyle(FormatStyle::LK_Cpp)); + verifyFormat("A> a;", getChromiumStyle()); // template closer followed by a token that starts with > or = verifyFormat("bool b = a<1> > 1;"); @@ -12814,7 +12814,7 @@ TEST_F(FormatTest, UnderstandsSquareAttributes) { } TEST_F(FormatTest, AttributeClass) { - FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); + FormatStyle Style = getChromiumStyle(); verifyFormat("class S {\n" " S(S&&) = default;\n" "};", @@ -15732,7 +15732,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { "#define a \\\n" " if \\\n" " 0", - getChromiumStyle(FormatStyle::LK_Cpp)); + getChromiumStyle()); } TEST_F(FormatTest, FormatStarDependingOnContext) { @@ -22542,7 +22542,7 @@ TEST_F(FormatTest, UnderstandsPragmas) { "#pragma comment(linker, \\\n" " \"argument\" \\\n" " \"argument\"", - getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32)); + getStyleWithColumns(getChromiumStyle(), 32)); } TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { @@ -26157,7 +26157,7 @@ TEST_F(FormatTest, GoogleDefaultStyle) { Style); } TEST_F(FormatTest, ChromiumDefaultStyle) { - FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); + FormatStyle Style = getChromiumStyle(); verifyFormat("extern \"C\" {\n" "int foo();\n" "}", diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 91577b9a49167..e1700cd24bb86 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -562,7 +562,7 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) { " b: 'bb'\n" "};"); - verifyFormat("f({a: 1, b: 2, c: 3});", + verifyFormat("f({a: 1, b: 2, c: 3});\n", getChromiumStyle(FormatStyle::LK_JavaScript)); verifyFormat("f({'a': [{}]});"); } diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp index e01c1d6d7e684..19782105140fd 100644 --- a/clang/unittests/Format/FormatTestJava.cpp +++ b/clang/unittests/Format/FormatTestJava.cpp @@ -60,7 +60,7 @@ TEST_F(FormatTestJava
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
https://github.com/victorvianna deleted https://github.com/llvm/llvm-project/pull/140888 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
@@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve; + ChromiumStyle.InsertBraces = true; victorvianna wrote: Done https://github.com/llvm/llvm-project/pull/140888 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
@@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve; + ChromiumStyle.InsertBraces = true; victorvianna wrote: Ack, abandoning then https://github.com/llvm/llvm-project/pull/140888 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
https://github.com/victorvianna closed https://github.com/llvm/llvm-project/pull/140888 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
@@ -5904,7 +5904,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { getLLVMStyleWithColumns(60)); verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " - "(n, buffers))", + "(n, buffers))\n", getChromiumStyle(FormatStyle::LK_Cpp)); victorvianna wrote: Done https://github.com/llvm/llvm-project/pull/140888 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] ClangFormat: Insert braces & newline for Chromium (PR #140888)
https://github.com/victorvianna updated https://github.com/llvm/llvm-project/pull/140888 >From cf4f52fc09333a2de6339758bb68f464d2971bec Mon Sep 17 00:00:00 2001 From: Victor Hugo Vianna Silva Date: Wed, 21 May 2025 14:48:13 +0100 Subject: [PATCH] ClangFormat: Insert braces & newline for Chromium This addresses an old TODO in the Chromium codebase to upstream a change to the Chromium format style. https://source.chromium.org/chromium/chromium/src/+/main:.clang-format;l=10;drc=b9d8d6aeeef9feacf6eb4838cdccca4c2da2a0eb Bug: crbug.com/40247920 --- clang/lib/Format/Format.cpp | 3 + clang/unittests/Format/FormatTest.cpp | 74 +++ clang/unittests/Format/FormatTestJS.cpp | 2 +- clang/unittests/Format/FormatTestJava.cpp | 4 +- clang/unittests/Format/FormatTestObjC.cpp | 2 +- 5 files changed, 44 insertions(+), 41 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0cfa061681053..389a5f0ea0b5b 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { ChromiumStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Preserve; + ChromiumStyle.InsertBraces = true; + ChromiumStyle.InsertNewlineAtEOF = true; + if (Language == FormatStyle::LK_Java) { ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c0633ba3c29b3..e0664f46e1d72 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5420,7 +5420,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "#endif\n" "void bar() {\n" "int y = 0;\n" - "}", + "}\n", style); style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; @@ -5432,7 +5432,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "#endif\n" "void bar() {\n" "int y = 0;\n" - "}", + "}\n", style); style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; @@ -5444,7 +5444,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "#endif\n" "void bar() {\n" "int y = 0;\n" - "}", + "}\n", style); verifyFormat("#if 1\n" " // some comments\n" @@ -5454,7 +5454,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "void bar() {\n" "// comment\n" "int y = 0;\n" - "}", + "}\n", "#if 1\n" "// some comments\n" "// another\n" @@ -5463,7 +5463,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "void bar() {\n" " // comment\n" " int y = 0;\n" - "}", + "}\n", style); style.IndentPPDirectives = FormatStyle::PPDIS_None; @@ -5473,7 +5473,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "B(); \\\n" "} \\\n" "C();\n" - "#endif", + "#endif\n", style); verifyFormat("if (emacs) {\n" "#ifdef is\n" @@ -5482,7 +5482,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "return duh(); \\\n" "}\n" "#endif\n" - "}", + "}\n", style); verifyFormat("#if abc\n" "#ifdef foo\n" @@ -5494,7 +5494,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "}\\\n" "D();\n" "#endif\n" - "#endif", + "#endif\n", style); verifyFormat("#ifndef foo\n" "#define foo\n" @@ -5506,7 +5506,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "}\n" "#endif\n" "}\n" - "#endif", + "#endif\n", style); verifyFormat("#if 1\n" "#define X \\\n" @@ -5514,13 +5514,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { "x; \\\n" "x; \\\n" "}\n" - "#endif", + "#endif\n", style); verifyFormat("#define X \\\n" "{ \\\n" "x; \\\n" "x; \\\n" - "}", + "}\n", style); style.PPIndentWidth = 2; @@ -5530,7 +5530,7 @@ TEST_F