[clang] [clang-format] Option to insert spaces before the closing `*/` (PR #162105)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 28 15:02:05 PDT 2025


================
@@ -18,11 +18,66 @@
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Regex.h"
 
 namespace clang {
 namespace format {
 
+namespace {
+
+CommentKind classifyBlockComment(StringRef Text) {
+  if (!Text.starts_with("/*") || !Text.ends_with("*/"))
+    return CommentKind::Plain;
+  if (Text.starts_with("/**") || Text.starts_with("/*!"))
+    return CommentKind::DocString;
+  const StringRef Content = Text.drop_front(2).drop_back(2).trim();
+  if (Content.empty())
+    return CommentKind::Plain;
+
+  // Allow '$' in identifiers. This is required for languages like JavaScript
+  // which clang-format supports, to correctly classify parameter/sentinel
+  // comments such as /*$scope=*/ or /*$FALLTHROUGH*/.
+  const auto IsIdentifierStart = [](char C) {
+    return llvm::isAlpha(C) || C == '_' || C == '$';
+  };
+  const auto IsIdentifierBody = [](char C) {
+    return llvm::isAlnum(C) || C == '_' || C == '$';
+  };
+  const auto IsIdentifierLike = [&](StringRef Value) {
+    if (Value.empty())
+      return false;
+    if (!IsIdentifierStart(Value.front()))
+      return false;
+    for (char C : Value.drop_front())
+      if (!IsIdentifierBody(C))
+        return false;
+    return true;
+  };
+  const auto IsUppercaseWord = [](StringRef Value) {
+    if (Value.empty())
+      return false;
+    for (char C : Value) {
+      if (llvm::isUpper(C) || llvm::isDigit(C) || C == '_' || C == '$')
+        continue;
+      return false;
+    }
+    return true;
+  };
+  const bool HasWhitespace =
+      Content.find_first_of(" \t\n\v\f\r") != StringRef::npos;
+
+  if (!HasWhitespace && IsUppercaseWord(Content))
+    return CommentKind::Sentinel;
+  if (Content.ends_with('=')) {
----------------
HazardyKnusperkeks wrote:

I'd just stick with this condition, this is already used by clang-format on whether to add a space after the comment or not. It doesn't matter what's before the `=`.

https://github.com/llvm/llvm-project/pull/162105


More information about the cfe-commits mailing list