[clang] [Clang] Repair the function "rParenEndsCast" to make incorrect judgments in template variable cases (PR #120904)

via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 22 10:07:16 PST 2024


https://github.com/dty2 updated https://github.com/llvm/llvm-project/pull/120904

>From f36a48a92999cb791bf79b79adddaa73cab6f135 Mon Sep 17 00:00:00 2001
From: hunter <284050500 at qq.com>
Date: Mon, 23 Dec 2024 00:35:30 +0800
Subject: [PATCH 1/3] [Clang] Repair the functionrParenEndsCast to make
 incorrect judgments in template variable cases

---
 clang/lib/Format/TokenAnnotator.cpp | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f2cfa7f49f62f9..fa9751cc8a7d92 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -38,6 +38,9 @@ static bool mustBreakAfterAttributes(const FormatToken &Tok,
 
 namespace {
 
+SmallVector<llvm::StringRef, 100> castIdentifiers{"__type_identity_t",
+                                                  "remove_reference_t"};
+
 /// Returns \c true if the line starts with a token that can start a statement
 /// with an initializer.
 static bool startsWithInitStatement(const AnnotatedLine &Line) {
@@ -2474,6 +2477,9 @@ class AnnotatingParser {
           Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
         Current.setType(TT_StringInConcatenation);
       }
+    } else if (Current.is(tok::kw_using)) {
+      if (Current.Next->Next->Next->isTypeName(LangOpts))
+        castIdentifiers.push_back(Current.Next->TokenText);
     } else if (Current.is(tok::l_paren)) {
       if (lParenStartsCppCast(Current))
         Current.setType(TT_CppCastLParen);
@@ -2831,8 +2837,18 @@ class AnnotatingParser {
         IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
     bool ParensCouldEndDecl =
         AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
-    if (ParensAreType && !ParensCouldEndDecl)
+    if (ParensAreType && !ParensCouldEndDecl) {
+      if (BeforeRParen->is(TT_TemplateCloser)) {
+        auto *Prev = BeforeRParen->MatchingParen->getPreviousNonComment();
+        if (Prev) {
+          for (auto &name : castIdentifiers)
+            if (Prev->TokenText == name)
+              return true;
+          return false;
+        }
+      }
       return true;
+    }
 
     // At this point, we heuristically assume that there are no casts at the
     // start of the line. We assume that we have found most cases where there

>From 3cdd6fddfbdbf5a27fa2c6cf3c26c57435c78b70 Mon Sep 17 00:00:00 2001
From: hunter <284050500 at qq.com>
Date: Mon, 23 Dec 2024 01:55:15 +0800
Subject: [PATCH 2/3] [Clang] Repair the functionrParenEndsCast to make
 incorrect judgments in template variable cases

---
 clang/lib/Format/TokenAnnotator.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index fa9751cc8a7d92..ccf18bbdea84e0 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -38,6 +38,7 @@ static bool mustBreakAfterAttributes(const FormatToken &Tok,
 
 namespace {
 
+// TODO: Add new Type modifiers
 SmallVector<llvm::StringRef, 100> castIdentifiers{"__type_identity_t",
                                                   "remove_reference_t"};
 

>From ae3dbe95fc9eaa4c5a6a59ad71cd6db845f68509 Mon Sep 17 00:00:00 2001
From: hunter <284050500 at qq.com>
Date: Mon, 23 Dec 2024 02:06:59 +0800
Subject: [PATCH 3/3] [Clang] Repair the functionrParenEndsCast to make
 incorrect judgments in template variable cases

---
 clang/lib/Format/TokenAnnotator.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index ccf18bbdea84e0..8b19e18a3d7ef5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -17,6 +17,8 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Debug.h"
 
 #define DEBUG_TYPE "format-token-annotator"



More information about the cfe-commits mailing list