[clang] [clang-format] Don't count template template parameter as declaration (PR #95025)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 13 22:25:31 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:
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
More information about the cfe-commits
mailing list