Skip to content

Commit cf4f52f

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 cf4f52f

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
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: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,7 +5420,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54205420
"#endif\n"
54215421
"void bar() {\n"
54225422
" int y = 0;\n"
5423-
"}",
5423+
"}\n",
54245424
style);
54255425

54265426
style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
@@ -5432,7 +5432,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54325432
"#endif\n"
54335433
"void bar() {\n"
54345434
" int y = 0;\n"
5435-
"}",
5435+
"}\n",
54365436
style);
54375437

54385438
style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
@@ -5444,7 +5444,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54445444
"#endif\n"
54455445
"void bar() {\n"
54465446
" int y = 0;\n"
5447-
"}",
5447+
"}\n",
54485448
style);
54495449
verifyFormat("#if 1\n"
54505450
" // some comments\n"
@@ -5454,7 +5454,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54545454
"void bar() {\n"
54555455
" // comment\n"
54565456
" int y = 0;\n"
5457-
"}",
5457+
"}\n",
54585458
"#if 1\n"
54595459
"// some comments\n"
54605460
"// another\n"
@@ -5463,7 +5463,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54635463
"void bar() {\n"
54645464
" // comment\n"
54655465
" int y = 0;\n"
5466-
"}",
5466+
"}\n",
54675467
style);
54685468

54695469
style.IndentPPDirectives = FormatStyle::PPDIS_None;
@@ -5473,7 +5473,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54735473
" B(); \\\n"
54745474
" } \\\n"
54755475
" C();\n"
5476-
"#endif",
5476+
"#endif\n",
54775477
style);
54785478
verifyFormat("if (emacs) {\n"
54795479
"#ifdef is\n"
@@ -5482,7 +5482,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54825482
" return duh(); \\\n"
54835483
" }\n"
54845484
"#endif\n"
5485-
"}",
5485+
"}\n",
54865486
style);
54875487
verifyFormat("#if abc\n"
54885488
"#ifdef foo\n"
@@ -5494,7 +5494,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
54945494
" } \\\n"
54955495
" D();\n"
54965496
"#endif\n"
5497-
"#endif",
5497+
"#endif\n",
54985498
style);
54995499
verifyFormat("#ifndef foo\n"
55005500
"#define foo\n"
@@ -5506,21 +5506,21 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55065506
" }\n"
55075507
"#endif\n"
55085508
"}\n"
5509-
"#endif",
5509+
"#endif\n",
55105510
style);
55115511
verifyFormat("#if 1\n"
55125512
"#define X \\\n"
55135513
" { \\\n"
55145514
" x; \\\n"
55155515
" x; \\\n"
55165516
" }\n"
5517-
"#endif",
5517+
"#endif\n",
55185518
style);
55195519
verifyFormat("#define X \\\n"
55205520
" { \\\n"
55215521
" x; \\\n"
55225522
" x; \\\n"
5523-
" }",
5523+
" }\n",
55245524
style);
55255525

55265526
style.PPIndentWidth = 2;
@@ -5530,7 +5530,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55305530
" B(); \\\n"
55315531
" } \\\n"
55325532
" C();\n"
5533-
"#endif",
5533+
"#endif\n",
55345534
style);
55355535
style.IndentWidth = 8;
55365536
verifyFormat("#ifdef foo\n"
@@ -5539,7 +5539,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55395539
" B(); \\\n"
55405540
" } \\\n"
55415541
" C();\n"
5542-
"#endif",
5542+
"#endif\n",
55435543
style);
55445544

55455545
style.IndentWidth = 1;
@@ -5550,13 +5550,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55505550
" x; \\\n"
55515551
" x; \\\n"
55525552
" }\n"
5553-
"#endif",
5553+
"#endif\n",
55545554
style);
55555555
verifyFormat("#define X \\\n"
55565556
" { \\\n"
55575557
" x; \\\n"
55585558
" x; \\\n"
5559-
" }",
5559+
" }\n",
55605560
style);
55615561

55625562
style.IndentWidth = 4;
@@ -5568,7 +5568,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55685568
" B(); \\\n"
55695569
" } \\\n"
55705570
" C();\n"
5571-
"#endif",
5571+
"#endif\n",
55725572
style);
55735573
verifyFormat("#if abc\n"
55745574
"# ifdef foo\n"
@@ -5580,7 +5580,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55805580
" } \\\n"
55815581
" D();\n"
55825582
"# endif\n"
5583-
"#endif",
5583+
"#endif\n",
55845584
style);
55855585
verifyFormat("#ifndef foo\n"
55865586
"#define foo\n"
@@ -5592,13 +5592,13 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
55925592
" }\n"
55935593
"#endif\n"
55945594
"}\n"
5595-
"#endif",
5595+
"#endif\n",
55965596
style);
55975597
verifyFormat("#define X \\\n"
55985598
" { \\\n"
55995599
" x; \\\n"
56005600
" x; \\\n"
5601-
" }",
5601+
" }\n",
56025602
style);
56035603

56045604
style.PPIndentWidth = 2;
@@ -5609,7 +5609,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56095609
" B(); \\\n"
56105610
" } \\\n"
56115611
" C();\n"
5612-
"#endif",
5612+
"#endif\n",
56135613
style);
56145614

56155615
style.PPIndentWidth = 4;
@@ -5618,7 +5618,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56185618
" { \\\n"
56195619
" x; \\\n"
56205620
" x; \\\n"
5621-
" }",
5621+
" }\n",
56225622
style);
56235623

56245624
style.IndentWidth = 4;
@@ -5631,7 +5631,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56315631
" return duh(); \\\n"
56325632
" }\n"
56335633
"#endif\n"
5634-
"}",
5634+
"}\n",
56355635
style);
56365636
verifyFormat("#if abc\n"
56375637
" #ifdef foo\n"
@@ -5641,15 +5641,15 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56415641
" } \\\n"
56425642
" C();\n"
56435643
" #endif\n"
5644-
"#endif",
5644+
"#endif\n",
56455645
style);
56465646
verifyFormat("#if 1\n"
56475647
" #define X \\\n"
56485648
" { \\\n"
56495649
" x; \\\n"
56505650
" x; \\\n"
56515651
" }\n"
5652-
"#endif",
5652+
"#endif\n",
56535653
style);
56545654

56555655
style.PPIndentWidth = 2;
@@ -5659,7 +5659,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56595659
" B(); \\\n"
56605660
" } \\\n"
56615661
" C();\n"
5662-
"#endif",
5662+
"#endif\n",
56635663
style);
56645664

56655665
style.PPIndentWidth = 4;
@@ -5670,7 +5670,7 @@ TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
56705670
" x; \\\n"
56715671
" x; \\\n"
56725672
" }\n"
5673-
"#endif",
5673+
"#endif\n",
56745674
style);
56755675
}
56765676

@@ -5904,7 +5904,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
59045904
getLLVMStyleWithColumns(60));
59055905

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

59105910
// See PR41483
@@ -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;\n", getChromiumStyle(FormatStyle::LK_Cpp));
1160911609

1161011610
// template closer followed by a token that starts with > or =
1161111611
verifyFormat("bool b = a<1> > 1;");
@@ -12817,23 +12817,23 @@ TEST_F(FormatTest, AttributeClass) {
1281712817
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
1281812818
verifyFormat("class S {\n"
1281912819
" S(S&&) = default;\n"
12820-
"};",
12820+
"};\n",
1282112821
Style);
1282212822
verifyFormat("class [[nodiscard]] S {\n"
1282312823
" S(S&&) = default;\n"
12824-
"};",
12824+
"};\n",
1282512825
Style);
1282612826
verifyFormat("class __attribute((maybeunused)) S {\n"
1282712827
" S(S&&) = default;\n"
12828-
"};",
12828+
"};\n",
1282912829
Style);
1283012830
verifyFormat("struct S {\n"
1283112831
" S(S&&) = default;\n"
12832-
"};",
12832+
"};\n",
1283312833
Style);
1283412834
verifyFormat("struct [[nodiscard]] S {\n"
1283512835
" S(S&&) = default;\n"
12836-
"};",
12836+
"};\n",
1283712837
Style);
1283812838
}
1283912839

@@ -15731,7 +15731,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
1573115731
verifyFormat("//\n"
1573215732
"#define a \\\n"
1573315733
" if \\\n"
15734-
" 0",
15734+
" 0\n",
1573515735
getChromiumStyle(FormatStyle::LK_Cpp));
1573615736
}
1573715737

@@ -22538,10 +22538,10 @@ TEST_F(FormatTest, UnderstandsPragmas) {
2253822538

2253922539
verifyFormat("#pragma comment(linker, \\\n"
2254022540
" \"argument\" \\\n"
22541-
" \"argument\"",
22541+
" \"argument\"\n",
2254222542
"#pragma comment(linker, \\\n"
2254322543
" \"argument\" \\\n"
22544-
" \"argument\"",
22544+
" \"argument\"\n",
2254522545
getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32));
2254622546
}
2254722547

@@ -26160,7 +26160,7 @@ TEST_F(FormatTest, ChromiumDefaultStyle) {
2616026160
FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
2616126161
verifyFormat("extern \"C\" {\n"
2616226162
"int foo();\n"
26163-
"}",
26163+
"}\n",
2616426164
Style);
2616526165
}
2616626166
TEST_F(FormatTest, MicrosoftDefaultStyle) {

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)

0 commit comments

Comments
 (0)