[clang] 059f2df - [clang-format] Fix an assertion failure on comment-only config files (#163111)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 16 21:41:03 PDT 2025


Author: owenca
Date: 2025-10-16T21:40:59-07:00
New Revision: 059f2df74898ff7f394dc8e60f746323499ae32b

URL: https://github.com/llvm/llvm-project/commit/059f2df74898ff7f394dc8e60f746323499ae32b
DIFF: https://github.com/llvm/llvm-project/commit/059f2df74898ff7f394dc8e60f746323499ae32b.diff

LOG: [clang-format] Fix an assertion failure on comment-only config files (#163111)

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 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;


        


More information about the cfe-commits mailing list