[clang-format] [PR52527] can join * with /* to form an outside of comment error C4138
authormydeveloperday <mydeveloperday@gmail.com>
Tue, 23 Nov 2021 10:35:05 +0000 (10:35 +0000)
committermydeveloperday <mydeveloperday@gmail.com>
Tue, 23 Nov 2021 10:36:06 +0000 (10:36 +0000)
https://bugs.llvm.org/show_bug.cgi?id=52527

The follow patch ensures there is always a space between * and /* to prevent transforming
```
void foo(* /* comment */)(int bar);
```
into
```
void foo(*/* comment */)(int bar);
```

Differential Revision: https://reviews.llvm.org/D114142

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index f3f63b4..817bda1 100644 (file)
@@ -3246,6 +3246,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
   };
   if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
     return true; // Never ever merge two identifiers.
+
+  // Leave a space between * and /* to avoid C4138 `comment end` found outside
+  // of comment.
+  if (Left.is(tok::star) && Right.is(tok::comment))
+    return true;
+
   if (Style.isCpp()) {
     if (Left.is(tok::kw_operator))
       return Right.is(tok::coloncolon);
index 6507cbe..8c8d7a4 100644 (file)
@@ -9439,6 +9439,13 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
   verifyFormat("void f() { &(*I).first; }");
 
   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
+  verifyFormat("f(* /* confusing comment */ foo);");
+  verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
+  verifyFormat("void foo(int * // this is the first paramters\n"
+               "         ,\n"
+               "         int second);");
+  verifyFormat("double term = a * // first\n"
+               "              b;");
   verifyFormat(
       "int *MyValues = {\n"
       "    *A, // Operator detection might be confused by the '{'\n"