[PATCH] D77282: [AST] Fix formatting whitespace-only comments

Ryan Gonzalez via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 1 17:59:13 PDT 2020


refi64 created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, ilya-biryukov.
Herald added a project: clang.
refi64 added a reviewer: ilya-biryukov.
refi64 added a comment.

(I manually ran clang-format-diff afterwards, there were no suggested changes.)


If a string consisted only of whitespace, the emptiness check in
getFormattedText would fail, but DropTrailingNewLines would then
call Str.back() on an empty string. In practice, this can easily
crash clangd if an editor asks for a declaration comment that is
whitespace-only.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77282

Files:
  clang/lib/AST/RawCommentList.cpp
  clang/unittests/AST/CommentTextTest.cpp


Index: clang/unittests/AST/CommentTextTest.cpp
===================================================================
--- clang/unittests/AST/CommentTextTest.cpp
+++ clang/unittests/AST/CommentTextTest.cpp
@@ -124,4 +124,10 @@
   // clang-format on
 }
 
+TEST_F(CommentTextTest, WorksWithEmptyBlockComments) {
+  auto ExpectedOutput = "";
+  auto Formatted = formatComment(R"(/* */)");
+  EXPECT_EQ(ExpectedOutput, Formatted);
+}
+
 } // namespace clang
Index: clang/lib/AST/RawCommentList.cpp
===================================================================
--- clang/lib/AST/RawCommentList.cpp
+++ clang/lib/AST/RawCommentList.cpp
@@ -431,7 +431,7 @@
   };
 
   auto DropTrailingNewLines = [](std::string &Str) {
-    while (Str.back() == '\n')
+    while (!Str.empty() && Str.back() == '\n')
       Str.pop_back();
   };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77282.254380.patch
Type: text/x-patch
Size: 826 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200402/01c354ff/attachment.bin>


More information about the cfe-commits mailing list