diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -779,11 +779,14 @@ const char FirstCommentChar = Lines[i][IndentPrefix.size()]; const unsigned FirstCharByteSize = encoding::getCodePointNumBytes(FirstCommentChar, Encoding); - return encoding::columnWidth( - Lines[i].substr(IndentPrefix.size(), FirstCharByteSize), - Encoding) == 1 && - (FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) || - isHorizontalWhitespace(FirstCommentChar)); + if (encoding::columnWidth( + Lines[i].substr(IndentPrefix.size(), FirstCharByteSize), + Encoding) != 1) + return false; + if (FirstCommentChar == '#') + return false; + return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) || + isHorizontalWhitespace(FirstCommentChar); }; // On the first line of the comment section we calculate how many spaces diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -91,6 +91,9 @@ "// line 2\n" "void f() {}\n"); + EXPECT_EQ("// comment\n", format("//comment\n")); + EXPECT_EQ("// #comment\n", format("//#comment\n")); + EXPECT_EQ("// comment\n" "// clang-format on\n", format("//comment\n"