[clang-tools-extra] [clangd] Do not show `aParam` parameter hint for argument spelled `param` (PR #119162)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 8 19:38:18 PST 2024


https://github.com/HighCommander4 created https://github.com/llvm/llvm-project/pull/119162

Fixes https://github.com/clangd/clangd/issues/2248

>From c207c5d0d651b564acec0a0b6ef257fb89752ba0 Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Mon, 21 Oct 2024 02:34:15 -0400
Subject: [PATCH] [clangd] Do not show `aParam` parameter hint for argument
 spelled `param`

Fixes https://github.com/clangd/clangd/issues/2248
---
 clang-tools-extra/clangd/InlayHints.cpp         | 17 ++++++++++++++++-
 .../clangd/unittests/InlayHintTests.cpp         |  9 +++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index fefffeb4efc1a2..0ad0ffd86bd4c8 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -867,13 +867,28 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
     }
   }
 
+  static bool argumentMatchesParamName(StringRef ArgName, StringRef ParamName) {
+    // Exact match.
+    if (ParamName == ArgName)
+      return true;
+
+    // Parameter name uses "a" prefix (e.g. "aParam").
+    if (ParamName.size() > 1 && ParamName[0] == 'a' &&
+        std::isupper(ParamName[1])) {
+      return ArgName.size() > 0 && ArgName[0] == std::tolower(ParamName[1]) &&
+             ArgName.drop_front() == ParamName.drop_front(2);
+    }
+
+    return false;
+  }
+
   bool shouldHintName(const Expr *Arg, StringRef ParamName) {
     if (ParamName.empty())
       return false;
 
     // If the argument expression is a single name and it matches the
     // parameter name exactly, omit the name hint.
-    if (ParamName == getSpelledIdentifier(Arg))
+    if (argumentMatchesParamName(getSpelledIdentifier(Arg), ParamName))
       return false;
 
     // Exclude argument expressions preceded by a /*paramName*/.
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 77d78b8777fe30..3f9795c2330b85 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1011,6 +1011,7 @@ TEST(ParameterHints, FunctionPointer) {
 TEST(ParameterHints, ArgMatchesParam) {
   assertParameterHints(R"cpp(
     void foo(int param);
+    void prefixConvention(int aParam);
     struct S {
       static const int param = 42;
     };
@@ -1018,6 +1019,12 @@ TEST(ParameterHints, ArgMatchesParam) {
       int param = 42;
       // Do not show redundant "param: param".
       foo(param);
+      // Some codebases have a naming convention of prefixing
+      // parameter names with "a", e.g. "aParam". (The "a"
+      // stands for "argument", used as an (imprecise) synonym
+      // for "parameter".)
+      // Do not show "aParam: param" either.
+      prefixConvention(param);
       // But show it if the argument is qualified.
       foo($param[[S::param]]);
     }
@@ -1026,6 +1033,8 @@ TEST(ParameterHints, ArgMatchesParam) {
       void bar() {
         // Do not show "param: param" for member-expr.
         foo(param);
+        // Nor "aParam: param"
+        prefixConvention(param);
       }
     };
   )cpp",



More information about the cfe-commits mailing list