Skip to content

Commit 8409918

Browse files
committed
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
1 parent 9f8d798 commit 8409918

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,9 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
18921892
ChromiumStyle.IncludeStyle.IncludeBlocks =
18931893
tooling::IncludeStyle::IBS_Preserve;
18941894

1895+
ChromiumStyle.InsertBraces = true;
1896+
ChromiumStyle.InsertNewlineAtEOF = true;
1897+
18951898
if (Language == FormatStyle::LK_Java) {
18961899
ChromiumStyle.AllowShortIfStatementsOnASingleLine =
18971900
FormatStyle::SIS_WithoutElse;

clang/unittests/Format/FormatTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5407,7 +5407,7 @@ TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
54075407
}
54085408

54095409
TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5410-
FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5410+
FormatStyle style = getChromiumStyle();
54115411
style.IndentWidth = 4;
54125412
style.PPIndentWidth = 1;
54135413

@@ -5905,7 +5905,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
59055905

59065906
verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
59075907
"(n, buffers))",
5908-
getChromiumStyle(FormatStyle::LK_Cpp));
5908+
getChromiumStyle());
59095909

59105910
// See PR41483
59115911
verifyNoChange("/**/ FOO(a)\n"
@@ -11605,7 +11605,7 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
1160511605
verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
1160611606
getGoogleStyle());
1160711607

11608-
verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11608+
verifyFormat("A<A<int>> a;", getChromiumStyle());
1160911609

1161011610
// template closer followed by a token that starts with > or =
1161111611
verifyFormat("bool b = a<1> > 1;");
@@ -12814,7 +12814,7 @@ TEST_F(FormatTest, UnderstandsSquareAttributes) {
1281412814
}
1281512815

1281612816
TEST_F(FormatTest, AttributeClass) {
12817-
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12817+
FormatStyle Style = getChromiumStyle();
1281812818
verifyFormat("class S {\n"
1281912819
" S(S&&) = default;\n"
1282012820
"};",
@@ -15732,7 +15732,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1573215732
"#define a \\\n"
1573315733
" if \\\n"
1573415734
" 0",
15735-
getChromiumStyle(FormatStyle::LK_Cpp));
15735+
getChromiumStyle());
1573615736
}
1573715737

1573815738
TEST_F(FormatTest, FormatStarDependingOnContext) {
@@ -22542,7 +22542,7 @@ TEST_F(FormatTest, UnderstandsPragmas) {
2254222542
"#pragma comment(linker, \\\n"
2254322543
" \"argument\" \\\n"
2254422544
" \"argument\"",
22545-
getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32));
22545+
getStyleWithColumns(getChromiumStyle(), 32));
2254622546
}
2254722547

2254822548
TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
@@ -26157,7 +26157,7 @@ TEST_F(FormatTest, GoogleDefaultStyle) {
2615726157
Style);
2615826158
}
2615926159
TEST_F(FormatTest, ChromiumDefaultStyle) {
26160-
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
26160+
FormatStyle Style = getChromiumStyle();
2616126161
verifyFormat("extern \"C\" {\n"
2616226162
"int foo();\n"
2616326163
"}",

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) {
562562
" b: 'bbbbbbbbbbbbbbbbbb'\n"
563563
"};");
564564

565-
verifyFormat("f({a: 1, b: 2, c: 3});",
565+
verifyFormat("f({a: 1, b: 2, c: 3});\n",
566566
getChromiumStyle(FormatStyle::LK_JavaScript));
567567
verifyFormat("f({'a': [{}]});");
568568
}

clang/unittests/Format/FormatTestJava.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TEST_F(FormatTestJava, Chromium) {
6060
" for (;;) f();\n"
6161
" if (true) f();\n"
6262
" }\n"
63-
"}",
63+
"}\n",
6464
getChromiumStyle(FormatStyle::LK_Java));
6565
}
6666

@@ -280,7 +280,7 @@ TEST_F(FormatTestJava, Annotations) {
280280
verifyFormat("@Partial @Mock DataLoader loader;");
281281
verifyFormat("@Partial\n"
282282
"@Mock\n"
283-
"DataLoader loader;",
283+
"DataLoader loader;\n",
284284
getChromiumStyle(FormatStyle::LK_Java));
285285
verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
286286
"public static int iiiiiiiiiiiiiiiiiiiiiiii;");

clang/unittests/Format/FormatTestObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
951951
" styleMask:NSBorderlessWindowMask\n"
952952
" backing:NSBackingStoreBuffered\n"
953953
" defer:NO]);\n"
954-
"}");
954+
"}\n");
955955

956956
// Respect continuation indent and colon alignment (e.g. when object name is
957957
// short, and first selector is the longest one)

clang/unittests/Format/FormatTestUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ inline FormatStyle getGoogleStyle() {
2424
return getGoogleStyle(FormatStyle::LK_Cpp);
2525
}
2626

27+
inline FormatStyle getChromiumStyle() {
28+
auto Style = getChromiumStyle(FormatStyle::LK_Cpp);
29+
Style.InsertNewlineAtEOF = false;
30+
return Style;
31+
}
32+
2733
// When HandleHash is false, preprocessor directives starting with hash will not
2834
// be on separate lines. This is needed because Verilog uses hash for other
2935
// purposes.

0 commit comments

Comments
 (0)