Skip to content

Commit c1c4d71

Browse files
owencatstellar
authored andcommitted
[clang-format] Allow Language: Cpp for C files (llvm#133033)
Fix llvm#132832 (cherry picked from commit 05fb840)
1 parent 2406e0d commit c1c4d71

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

clang/lib/Format/Format.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,10 +2114,14 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
21142114
FormatStyle::FormatStyleSet StyleSet;
21152115
bool LanguageFound = false;
21162116
for (const FormatStyle &Style : llvm::reverse(Styles)) {
2117-
if (Style.Language != FormatStyle::LK_None)
2117+
const auto Lang = Style.Language;
2118+
if (Lang != FormatStyle::LK_None)
21182119
StyleSet.Add(Style);
2119-
if (Style.Language == Language)
2120+
if (Lang == Language ||
2121+
// For backward compatibility.
2122+
(Lang == FormatStyle::LK_Cpp && Language == FormatStyle::LK_C)) {
21202123
LanguageFound = true;
2124+
}
21212125
}
21222126
if (!LanguageFound) {
21232127
if (Styles.empty() || Styles[0].Language != FormatStyle::LK_None)
@@ -2157,8 +2161,14 @@ FormatStyle::FormatStyleSet::Get(FormatStyle::LanguageKind Language) const {
21572161
if (!Styles)
21582162
return std::nullopt;
21592163
auto It = Styles->find(Language);
2160-
if (It == Styles->end())
2161-
return std::nullopt;
2164+
if (It == Styles->end()) {
2165+
if (Language != FormatStyle::LK_C)
2166+
return std::nullopt;
2167+
// For backward compatibility.
2168+
It = Styles->find(FormatStyle::LK_Cpp);
2169+
if (It == Styles->end())
2170+
return std::nullopt;
2171+
}
21622172
FormatStyle Style = It->second;
21632173
Style.StyleSet = *this;
21642174
return Style;

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,26 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
12141214
IndentWidth, 56u);
12151215
}
12161216

1217+
TEST(ConfigParseTest, AllowCppForC) {
1218+
FormatStyle Style = {};
1219+
Style.Language = FormatStyle::LK_C;
1220+
EXPECT_EQ(parseConfiguration("Language: Cpp", &Style), ParseError::Success);
1221+
1222+
CHECK_PARSE("---\n"
1223+
"IndentWidth: 4\n"
1224+
"---\n"
1225+
"Language: Cpp\n"
1226+
"IndentWidth: 8\n",
1227+
IndentWidth, 8u);
1228+
1229+
EXPECT_EQ(parseConfiguration("---\n"
1230+
"Language: ObjC\n"
1231+
"---\n"
1232+
"Language: Cpp\n",
1233+
&Style),
1234+
ParseError::Success);
1235+
}
1236+
12171237
TEST(ConfigParseTest, UsesLanguageForBasedOnStyle) {
12181238
FormatStyle Style = {};
12191239
Style.Language = FormatStyle::LK_JavaScript;

0 commit comments

Comments
 (0)