[clang] 0374bbb - [clang-format] Add a new style for the clang-format source code (#69814)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 22 23:01:31 PDT 2023
Author: Owen Pan
Date: 2023-10-22T23:01:27-07:00
New Revision: 0374bbba4c455e5f862a32581cc8d37690fb3b60
URL: https://github.com/llvm/llvm-project/commit/0374bbba4c455e5f862a32581cc8d37690fb3b60
DIFF: https://github.com/llvm/llvm-project/commit/0374bbba4c455e5f862a32581cc8d37690fb3b60.diff
LOG: [clang-format] Add a new style for the clang-format source code (#69814)
Added:
Modified:
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/unittests/Format/ConfigParseTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index ed92ef6fc655522..4c344135d25163c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4861,6 +4861,8 @@ FormatStyle getGNUStyle();
/// https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language);
+FormatStyle getClangFormatStyle();
+
/// Returns style indicating formatting should be not applied at all.
FormatStyle getNoStyle();
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index acbed56a86e141f..ff7cb097a59d6a0 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -834,8 +834,8 @@ template <> struct MappingTraits<FormatStyle> {
StringRef BasedOnStyle;
if (IO.outputting()) {
- StringRef Styles[] = {"LLVM", "Google", "Chromium", "Mozilla",
- "WebKit", "GNU", "Microsoft"};
+ StringRef Styles[] = {"LLVM", "Google", "Chromium", "Mozilla",
+ "WebKit", "GNU", "Microsoft", "clang-format"};
for (StringRef StyleName : Styles) {
FormatStyle PredefinedStyle;
if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
@@ -1915,6 +1915,16 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
return Style;
}
+FormatStyle getClangFormatStyle() {
+ FormatStyle Style = getLLVMStyle();
+ Style.InsertBraces = true;
+ Style.InsertNewlineAtEOF = true;
+ Style.LineEnding = FormatStyle::LE_LF;
+ Style.RemoveBracesLLVM = true;
+ Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
+ return Style;
+}
+
FormatStyle getNoStyle() {
FormatStyle NoStyle = getLLVMStyle();
NoStyle.DisableFormat = true;
@@ -1939,6 +1949,8 @@ bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language,
*Style = getGNUStyle();
else if (Name.equals_insensitive("microsoft"))
*Style = getMicrosoftStyle(Language);
+ else if (Name.equals_insensitive("clang-format"))
+ *Style = getClangFormatStyle();
else if (Name.equals_insensitive("none"))
*Style = getNoStyle();
else if (Name.equals_insensitive("inheritparentconfig"))
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index c35c82955f6a558..ba79c8d72f09552 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -63,6 +63,13 @@ TEST(ConfigParseTest, GetsPredefinedStyleByName) {
EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
EXPECT_ALL_STYLES_EQUAL(Styles);
+ Styles[0] = getClangFormatStyle();
+ EXPECT_TRUE(
+ getPredefinedStyle("clang-format", FormatStyle::LK_Cpp, &Styles[1]));
+ EXPECT_TRUE(
+ getPredefinedStyle("Clang-format", FormatStyle::LK_Cpp, &Styles[2]));
+ EXPECT_ALL_STYLES_EQUAL(Styles);
+
EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
}
More information about the cfe-commits
mailing list