Author: Owen Pan Date: 2025-03-27T01:00:02-07:00 New Revision: 05fb8408de23c3ccb6125b6886742177755bd757
URL: https://github.com/llvm/llvm-project/commit/05fb8408de23c3ccb6125b6886742177755bd757 DIFF: https://github.com/llvm/llvm-project/commit/05fb8408de23c3ccb6125b6886742177755bd757.diff LOG: [clang-format] Allow `Language: Cpp` for C files (#133033) Fix #132832 Added: Modified: clang/lib/Format/Format.cpp clang/unittests/Format/ConfigParseTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c67db4752e87b..28aea86139e0d 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2123,10 +2123,14 @@ 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() || Styles[0].Language != FormatStyle::LK_None) @@ -2166,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 cc42642f99252..287191d04d885 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -1230,6 +1230,26 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) { IndentWidth, 56u); } +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) { FormatStyle Style = {}; Style.Language = FormatStyle::LK_JavaScript; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits