[clang] [clang-format] Don't count template template parameter as declaration (PR #95025)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 12 20:16:59 PDT 2024


================
@@ -1269,10 +1269,17 @@ class AnnotatingParser {
     if (CurrentToken && CurrentToken->is(tok::less)) {
       CurrentToken->setType(TT_TemplateOpener);
       next();
-      if (!parseAngle())
+      TemplateDeclarationDepth++;
+      if (!parseAngle()) {
+        TemplateDeclarationDepth--;
         return false;
-      if (CurrentToken)
+      }
+      TemplateDeclarationDepth--;
+      if (CurrentToken &&
+          (TemplateDeclarationDepth == 0 ||
+           !CurrentToken->isOneOf(tok::kw_typename, tok::kw_class))) {
----------------
owenca wrote:

> If parseAngle fails, wouldn't that cause InTemplateDeclaration to forever be true? Is that not an issue?

If `parseAngle()` returns false, it probably means that the template decl is ill-formed. If so, it doesn't matter if `InTemplateDeclaration` is set to false?

An alternative solution:
```
-  bool parseTemplateDeclaration() {
-    if (CurrentToken && CurrentToken->is(tok::less)) {
-      CurrentToken->setType(TT_TemplateOpener);
-      next();
-      if (!parseAngle())
-        return false;
-      if (CurrentToken)
-        CurrentToken->Previous->ClosesTemplateDeclaration = true;
-      return true;
-    }
-    return false;
+  bool parseTemplateDeclaration(bool InTemplateParameter) {
+    if (!CurrentToken || CurrentToken->isNot(tok::less))
+      return false;
+
+    InTemplateDeclaration = true;
+    CurrentToken->setType(TT_TemplateOpener);
+    next();
+
+    const bool WellFormed = parseAngle();
+    InTemplateDeclaration = InTemplateParameter;
+
+    if (!WellFormed)
+      return false;
+
+    if (CurrentToken && !InTemplateParameter)
+      CurrentToken->Previous->ClosesTemplateDeclaration = true;
+
+    return true;
   }
```

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


More information about the cfe-commits mailing list