[PATCH] D127859: [clangd] Don't add inlay hints on std::move/forward

Tobias Ribizel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 15 07:29:48 PDT 2022


upsj created this revision.
upsj added a reviewer: nridge.
Herald added subscribers: usaxena95, kadircet, arphaman.
Herald added a project: All.
upsj requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This removes parameter inlay hints from a few builtin functions like std::move/std::forward


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127859

Files:
  clang-tools-extra/clangd/InlayHints.cpp
  clang-tools-extra/clangd/unittests/InlayHintTests.cpp


Index: clang-tools-extra/clangd/unittests/InlayHintTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -555,6 +555,17 @@
                        ExpectedHint{"timeout_millis: ", "timeout_millis"});
 }
 
+TEST(ParameterHints, BuiltinFunctions) {
+  // This prototype of std::forward is sufficient for clang to recognize it
+  assertParameterHints(R"cpp(
+    namespace std { template <typename T> T&& forward(T&); }
+    void foo() {
+      int i;
+      std::forward(i);
+    }
+  )cpp");
+}
+
 TEST(ParameterHints, IncludeAtNonGlobalScope) {
   Annotations FooInc(R"cpp(
     void bar() { foo(42); }
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -14,6 +14,7 @@
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Basic/Builtins.h"
 #include "clang/Basic/SourceManager.h"
 #include "llvm/ADT/ScopeExit.h"
 
@@ -400,8 +401,9 @@
     NameVec ParameterNames = chooseParameterNames(Callee, ArgCount);
 
     // Exclude setters (i.e. functions with one argument whose name begins with
-    // "set"), as their parameter name is also not likely to be interesting.
-    if (isSetter(Callee, ParameterNames))
+    // "set"), and builtins like std::move/forward/... as their parameter name
+    // is also not likely to be interesting.
+    if (isSetter(Callee, ParameterNames) || isSimpleBuiltin(Callee))
       return;
 
     for (size_t I = 0; I < ArgCount; ++I) {
@@ -440,6 +442,21 @@
     return WhatItIsSetting.equals_insensitive(ParamNames[0]);
   }
 
+  // Checks if the callee is one of the builtins
+  // addressof, as_const, forward, move(_if_noexcept)
+  static bool isSimpleBuiltin(const FunctionDecl *Callee) {
+    switch (Callee->getBuiltinID()) {
+    case Builtin::BIaddressof:
+    case Builtin::BIas_const:
+    case Builtin::BIforward:
+    case Builtin::BImove:
+    case Builtin::BImove_if_noexcept:
+      return true;
+    default:
+      return false;
+    }
+  }
+
   bool shouldHintName(const Expr *Arg, StringRef ParamName) {
     if (ParamName.empty())
       return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127859.437162.patch
Type: text/x-patch
Size: 2381 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220615/95150ee6/attachment.bin>


More information about the cfe-commits mailing list