[clang] [clang-format] Allow `Language: Cpp` for C files (PR #133033)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 26 08:59:32 PDT 2025
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/133033
>From d03c3420218eed7c2db3a5c17abb6a0b3efbba87 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 25 Mar 2025 21:45:00 -0700
Subject: [PATCH 1/2] [clang-format] Allow `Language: Cpp` for C files
Fix #132832
---
clang/lib/Format/Format.cpp | 13 ++++++++++---
clang/unittests/Format/ConfigParseTest.cpp | 6 ++++++
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c67db4752e87b..87e36578d9958 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2129,10 +2129,17 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
LanguageFound = true;
}
if (!LanguageFound) {
- if (Styles.empty() || Styles[0].Language != FormatStyle::LK_None)
+ if (Styles.empty())
return make_error_code(ParseError::Unsuitable);
- FormatStyle DefaultStyle = Styles[0];
- DefaultStyle.Language = Language;
+ auto DefaultStyle = Styles[0];
+ auto &DefaultLanguage = DefaultStyle.Language;
+ if (DefaultLanguage != FormatStyle::LK_None &&
+ // For backward compatibility.
+ !(DefaultLanguage == FormatStyle::LK_Cpp &&
+ Language == FormatStyle::LK_C)) {
+ return make_error_code(ParseError::Unsuitable);
+ }
+ DefaultLanguage = Language;
StyleSet.Add(std::move(DefaultStyle));
}
*Style = *StyleSet.Get(Language);
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index cc42642f99252..4c17c1a50face 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1230,6 +1230,12 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
IndentWidth, 56u);
}
+TEST(ConfigParseTest, AllowCppForC) {
+ FormatStyle Style = {};
+ Style.Language = FormatStyle::LK_C;
+ EXPECT_EQ(parseConfiguration("Language: Cpp", &Style), ParseError::Success);
+}
+
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_JavaScript;
>From ab43a511b2d64582f55f7c245c96074ed9426823 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Wed, 26 Mar 2025 08:56:44 -0700
Subject: [PATCH 2/2] Also handle cases where `Cpp` is not the first language
---
clang/lib/Format/Format.cpp | 31 ++++++++++++----------
clang/unittests/Format/ConfigParseTest.cpp | 14 ++++++++++
2 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 87e36578d9958..28aea86139e0d 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2123,23 +2123,20 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
FormatStyle::FormatStyleSet StyleSet;
bool LanguageFound = false;
for (const FormatStyle &Style : llvm::reverse(Styles)) {
- if (Style.Language != FormatStyle::LK_None)
+ const auto Lang = Style.Language;
+ if (Lang != FormatStyle::LK_None)
StyleSet.Add(Style);
- if (Style.Language == Language)
+ if (Lang == Language ||
+ // For backward compatibility.
+ (Lang == FormatStyle::LK_Cpp && Language == FormatStyle::LK_C)) {
LanguageFound = true;
+ }
}
if (!LanguageFound) {
- if (Styles.empty())
- return make_error_code(ParseError::Unsuitable);
- auto DefaultStyle = Styles[0];
- auto &DefaultLanguage = DefaultStyle.Language;
- if (DefaultLanguage != FormatStyle::LK_None &&
- // For backward compatibility.
- !(DefaultLanguage == FormatStyle::LK_Cpp &&
- Language == FormatStyle::LK_C)) {
+ if (Styles.empty() || Styles[0].Language != FormatStyle::LK_None)
return make_error_code(ParseError::Unsuitable);
- }
- DefaultLanguage = Language;
+ FormatStyle DefaultStyle = Styles[0];
+ DefaultStyle.Language = Language;
StyleSet.Add(std::move(DefaultStyle));
}
*Style = *StyleSet.Get(Language);
@@ -2173,8 +2170,14 @@ FormatStyle::FormatStyleSet::Get(FormatStyle::LanguageKind Language) const {
if (!Styles)
return std::nullopt;
auto It = Styles->find(Language);
- if (It == Styles->end())
- return std::nullopt;
+ if (It == Styles->end()) {
+ if (Language != FormatStyle::LK_C)
+ return std::nullopt;
+ // For backward compatibility.
+ It = Styles->find(FormatStyle::LK_Cpp);
+ if (It == Styles->end())
+ return std::nullopt;
+ }
FormatStyle Style = It->second;
Style.StyleSet = *this;
return Style;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 4c17c1a50face..287191d04d885 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -1234,6 +1234,20 @@ TEST(ConfigParseTest, AllowCppForC) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_C;
EXPECT_EQ(parseConfiguration("Language: Cpp", &Style), ParseError::Success);
+
+ CHECK_PARSE("---\n"
+ "IndentWidth: 4\n"
+ "---\n"
+ "Language: Cpp\n"
+ "IndentWidth: 8\n",
+ IndentWidth, 8u);
+
+ EXPECT_EQ(parseConfiguration("---\n"
+ "Language: ObjC\n"
+ "---\n"
+ "Language: Cpp\n",
+ &Style),
+ ParseError::Success);
}
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
More information about the cfe-commits
mailing list