[clang-tools-extra] [clangd] Fix OverridePureVirtuals to preserve attributes and formatting (PR #184023)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 29 01:08:50 PDT 2026


================
@@ -101,30 +102,63 @@ std::string removePureVirtualSyntax(const std::string &MethodDecl,
   assert(!MethodDecl.empty());
 
   TokenStream TS = lex(MethodDecl, LangOpts);
+  auto Tokens = TS.tokens();
 
-  std::string DeclString;
-  for (const clangd::Token &Tk : TS.tokens()) {
-    if (Tk.Kind == clang::tok::raw_identifier && Tk.text() == "virtual")
+  // Find the pure-specifier (= 0) by searching backwards.
+  size_t PureEqualIndex = Tokens.size();
+  for (int J = static_cast<int>(Tokens.size()) - 1; J >= 1; --J) {
+    auto Kind = Tokens[J].Kind;
+    if (Kind == tok::eof || Kind == tok::semi || Kind == tok::comment)
       continue;
 
-    // If the ending two tokens are "= 0", we break here and we already have the
-    // method's string without the pure virtual syntax.
-    const auto &Next = Tk.next();
-    if (Next.next().Kind == tok::eof && Tk.Kind == clang::tok::equal &&
-        Next.text() == "0")
+    if (Tokens[J].text() == "0" && Tokens[J - 1].Kind == tok::equal)
+      PureEqualIndex = J - 1;
+
+    break;
----------------
timon-ul wrote:

I think this `for` loop is a bit...odd. I understand you are filtering out the end of the string, but the last `if` just feels wrong. Like what are you trying to express by the case of "we did not find `=0` but we break out of the loop anyway". I know it cannot happen but...then why do we have an `if`?

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


More information about the cfe-commits mailing list