From bf2337717fb66c2b520aa4e858a8c53fb19b6c2c Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 12 Oct 2025 16:55:10 -0700 Subject: [PATCH] [clang-format] Fix an assertion failure on comment-only config files --- clang/lib/Format/Format.cpp | 3 ++- clang/unittests/Format/ConfigParseTest.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 686e54128d372..093e88ffcc85d 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2184,8 +2184,9 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config, Input >> Styles; if (Input.error()) return Input.error(); + if (Styles.empty()) + return make_error_code(ParseError::Success); - assert(!Styles.empty()); const auto StyleCount = Styles.size(); // Start from the second style as (only) the first one may be the default. diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 6111e86ff7076..52f02c3ba8686 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -1264,6 +1264,13 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) { IndentWidth, 56u); } +TEST(ConfigParseTest, AllowCommentOnlyConfigFile) { + FormatStyle Style = {}; + Style.Language = FormatStyle::LK_Cpp; + EXPECT_EQ(parseConfiguration("#Language: C", &Style), ParseError::Success); + EXPECT_EQ(Style.Language, FormatStyle::LK_Cpp); +} + TEST(ConfigParseTest, AllowCppForC) { FormatStyle Style = {}; Style.Language = FormatStyle::LK_C;