Skip to content

Commit 7e153f5

Browse files
owencac-rhodes
authored andcommitted
[clang-format] Fix an assertion failure on comment-only config files (llvm#163111)
(cherry picked from commit 059f2df)
1 parent faca424 commit 7e153f5

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2132,8 +2132,9 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
21322132
Input >> Styles;
21332133
if (Input.error())
21342134
return Input.error();
2135+
if (Styles.empty())
2136+
return make_error_code(ParseError::Success);
21352137

2136-
assert(!Styles.empty());
21372138
const auto StyleCount = Styles.size();
21382139

21392140
// Start from the second style as (only) the first one may be the default.

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,13 @@ TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {
12491249
IndentWidth, 56u);
12501250
}
12511251

1252+
TEST(ConfigParseTest, AllowCommentOnlyConfigFile) {
1253+
FormatStyle Style = {};
1254+
Style.Language = FormatStyle::LK_Cpp;
1255+
EXPECT_EQ(parseConfiguration("#Language: C", &Style), ParseError::Success);
1256+
EXPECT_EQ(Style.Language, FormatStyle::LK_Cpp);
1257+
}
1258+
12521259
TEST(ConfigParseTest, AllowCppForC) {
12531260
FormatStyle Style = {};
12541261
Style.Language = FormatStyle::LK_C;

0 commit comments

Comments
 (0)