================
@@ -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:

Actually, checking the nesting level of the template brackets like you did is 
easier to understand. You just don't need to check the type-parameter keys IMO. 
Below is a rewrite of the function based on your solution:
```cpp
$ git diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 958b46c535a9..9be5c09a32dd 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1266,23 +1266,22 @@ private:
   }
 
   bool parseTemplateDeclaration() {
-    if (CurrentToken && CurrentToken->is(tok::less)) {
-      CurrentToken->setType(TT_TemplateOpener);
-      next();
-      TemplateDeclarationDepth++;
-      if (!parseAngle()) {
-        TemplateDeclarationDepth--;
-        return false;
-      }
-      TemplateDeclarationDepth--;
-      if (CurrentToken &&
-          (TemplateDeclarationDepth == 0 ||
-           !CurrentToken->isOneOf(tok::kw_typename, tok::kw_class))) {
-        CurrentToken->Previous->ClosesTemplateDeclaration = true;
-      }
-      return true;
-    }
-    return false;
+    if (!CurrentToken || CurrentToken->isNot(tok::less))
+      return false;
+
+    CurrentToken->setType(TT_TemplateOpener);
+    next();
+
+    TemplateDeclarationDepth++;
+    const bool WellFormed = parseAngle();
+    TemplateDeclarationDepth--;
+    if (!WellFormed)
+      return false;
+
+    if (CurrentToken && TemplateDeclarationDepth == 0)
+      CurrentToken->Previous->ClosesTemplateDeclaration = true;
+
+    return true;
   }
 
   bool consumeToken() {
$ 
```

https://github.com/llvm/llvm-project/pull/95025
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to