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 <victor.viann...@gmail.com> 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(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " B(); \\\n" " } \\\n" " C();\n" - "#endif", + "#endif\n", style); style.IndentWidth = 8; verifyFormat("#ifdef foo\n" @@ -5539,7 +5539,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " B(); \\\n" " } \\\n" " C();\n" - "#endif", + "#endif\n", style); style.IndentWidth = 1; @@ -5550,13 +5550,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.IndentWidth = 4; @@ -5568,7 +5568,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " B(); \\\n" " } \\\n" " C();\n" - "#endif", + "#endif\n", style); verifyFormat("#if abc\n" "# ifdef foo\n" @@ -5580,7 +5580,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " } \\\n" " D();\n" "# endif\n" - "#endif", + "#endif\n", style); verifyFormat("#ifndef foo\n" "#define foo\n" @@ -5592,13 +5592,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " }\n" "#endif\n" "}\n" - "#endif", + "#endif\n", style); verifyFormat("#define X \\\n" " { \\\n" " x; \\\n" " x; \\\n" - " }", + " }\n", style); style.PPIndentWidth = 2; @@ -5609,7 +5609,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " B(); \\\n" " } \\\n" " C();\n" - "#endif", + "#endif\n", style); style.PPIndentWidth = 4; @@ -5618,7 +5618,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " { \\\n" " x; \\\n" " x; \\\n" - " }", + " }\n", style); style.IndentWidth = 4; @@ -5631,7 +5631,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " return duh(); \\\n" " }\n" "#endif\n" - "}", + "}\n", style); verifyFormat("#if abc\n" " #ifdef foo\n" @@ -5641,7 +5641,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " } \\\n" " C();\n" " #endif\n" - "#endif", + "#endif\n", style); verifyFormat("#if 1\n" " #define X \\\n" @@ -5649,7 +5649,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " x; \\\n" " x; \\\n" " }\n" - "#endif", + "#endif\n", style); style.PPIndentWidth = 2; @@ -5659,7 +5659,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " B(); \\\n" " } \\\n" " C();\n" - "#endif", + "#endif\n", style); style.PPIndentWidth = 4; @@ -5670,7 +5670,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { " x; \\\n" " x; \\\n" " }\n" - "#endif", + "#endif\n", style); } @@ -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)); // See PR41483 @@ -11605,7 +11605,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()); - verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); + verifyFormat("A<A<int>> a;\n", getChromiumStyle(FormatStyle::LK_Cpp)); // template closer followed by a token that starts with > or = verifyFormat("bool b = a<1> > 1;"); @@ -12817,23 +12817,23 @@ TEST_F(FormatTest, AttributeClass) { FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); verifyFormat("class S {\n" " S(S&&) = default;\n" - "};", + "};\n", Style); verifyFormat("class [[nodiscard]] S {\n" " S(S&&) = default;\n" - "};", + "};\n", Style); verifyFormat("class __attribute((maybeunused)) S {\n" " S(S&&) = default;\n" - "};", + "};\n", Style); verifyFormat("struct S {\n" " S(S&&) = default;\n" - "};", + "};\n", Style); verifyFormat("struct [[nodiscard]] S {\n" " S(S&&) = default;\n" - "};", + "};\n", Style); } @@ -15731,7 +15731,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { verifyFormat("//\n" "#define a \\\n" " if \\\n" - " 0", + " 0\n", getChromiumStyle(FormatStyle::LK_Cpp)); } @@ -22538,10 +22538,10 @@ TEST_F(FormatTest, UnderstandsPragmas) { verifyFormat("#pragma comment(linker, \\\n" " \"argument\" \\\n" - " \"argument\"", + " \"argument\"\n", "#pragma comment(linker, \\\n" " \"argument\" \\\n" - " \"argument\"", + " \"argument\"\n", getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32)); } @@ -26160,7 +26160,7 @@ TEST_F(FormatTest, ChromiumDefaultStyle) { FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); verifyFormat("extern \"C\" {\n" "int foo();\n" - "}", + "}\n", Style); } TEST_F(FormatTest, MicrosoftDefaultStyle) { 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: 'bbbbbbbbbbbbbbbbbb'\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, Chromium) { " for (;;) f();\n" " if (true) f();\n" " }\n" - "}", + "}\n", getChromiumStyle(FormatStyle::LK_Java)); } @@ -280,7 +280,7 @@ TEST_F(FormatTestJava, Annotations) { verifyFormat("@Partial @Mock DataLoader loader;"); verifyFormat("@Partial\n" "@Mock\n" - "DataLoader loader;", + "DataLoader loader;\n", getChromiumStyle(FormatStyle::LK_Java)); verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n" "public static int iiiiiiiiiiiiiiiiiiiiiiii;"); diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index f7f73db62045c..22416bceb2265 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -951,7 +951,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodExpr) { " styleMask:NSBorderlessWindowMask\n" " backing:NSBackingStoreBuffered\n" " defer:NO]);\n" - "}"); + "}\n"); // Respect continuation indent and colon alignment (e.g. when object name is // short, and first selector is the longest one) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits