Skip to content

Commit 0374bbb

Browse files
authored
[clang-format] Add a new style for the clang-format source code (#69814)
1 parent cdf713c commit 0374bbb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

clang/include/clang/Format/Format.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4861,6 +4861,8 @@ FormatStyle getGNUStyle();
48614861
/// https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
48624862
FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language);
48634863

4864+
FormatStyle getClangFormatStyle();
4865+
48644866
/// Returns style indicating formatting should be not applied at all.
48654867
FormatStyle getNoStyle();
48664868

clang/lib/Format/Format.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,8 +834,8 @@ template <> struct MappingTraits<FormatStyle> {
834834

835835
StringRef BasedOnStyle;
836836
if (IO.outputting()) {
837-
StringRef Styles[] = {"LLVM", "Google", "Chromium", "Mozilla",
838-
"WebKit", "GNU", "Microsoft"};
837+
StringRef Styles[] = {"LLVM", "Google", "Chromium", "Mozilla",
838+
"WebKit", "GNU", "Microsoft", "clang-format"};
839839
for (StringRef StyleName : Styles) {
840840
FormatStyle PredefinedStyle;
841841
if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
@@ -1915,6 +1915,16 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
19151915
return Style;
19161916
}
19171917

1918+
FormatStyle getClangFormatStyle() {
1919+
FormatStyle Style = getLLVMStyle();
1920+
Style.InsertBraces = true;
1921+
Style.InsertNewlineAtEOF = true;
1922+
Style.LineEnding = FormatStyle::LE_LF;
1923+
Style.RemoveBracesLLVM = true;
1924+
Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
1925+
return Style;
1926+
}
1927+
19181928
FormatStyle getNoStyle() {
19191929
FormatStyle NoStyle = getLLVMStyle();
19201930
NoStyle.DisableFormat = true;
@@ -1939,6 +1949,8 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
19391949
*Style = getGNUStyle();
19401950
else if (Name.equals_insensitive("microsoft"))
19411951
*Style = getMicrosoftStyle(Language);
1952+
else if (Name.equals_insensitive("clang-format"))
1953+
*Style = getClangFormatStyle();
19421954
else if (Name.equals_insensitive("none"))
19431955
*Style = getNoStyle();
19441956
else if (Name.equals_insensitive("inheritparentconfig"))

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ TEST(ConfigParseTest, GetsPredefinedStyleByName) {
6363
EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
6464
EXPECT_ALL_STYLES_EQUAL(Styles);
6565

66+
Styles[0] = getClangFormatStyle();
67+
EXPECT_TRUE(
68+
getPredefinedStyle("clang-format", FormatStyle::LK_Cpp, &Styles[1]));
69+
EXPECT_TRUE(
70+
getPredefinedStyle("Clang-format", FormatStyle::LK_Cpp, &Styles[2]));
71+
EXPECT_ALL_STYLES_EQUAL(Styles);
72+
6673
EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
6774
}
6875

0 commit comments

Comments
 (0)