[PATCH] D124359: [clangd] Add inlay hints for mutable reference parameters

Tobias Ribizel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 25 00:17:22 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.

Add a & or && annotation to all parameter inlay hints that refer to a non-const reference. That makes it easier to identify them even if semantic highlighting is not used (where this is already available)


https://reviews.llvm.org/D124359

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


Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -10,6 +10,7 @@
 #include "Config.h"
 #include "HeuristicResolver.h"
 #include "ParsedAST.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/RecursiveASTVisitor.h"
@@ -392,6 +393,7 @@
     // Don't show hints for variadic parameters.
     size_t FixedParamCount = getFixedParamCount(Callee);
     size_t ArgCount = std::min(FixedParamCount, Args.size());
+    auto Params = Callee->parameters();
 
     NameVec ParameterNames = chooseParameterNames(Callee, ArgCount);
 
@@ -402,12 +404,18 @@
 
     for (size_t I = 0; I < ArgCount; ++I) {
       StringRef Name = ParameterNames[I];
-      if (!shouldHint(Args[I], Name))
-        continue;
+      bool NameHint = shouldNameHint(Args[I], Name);
+      std::string Suffix = ": ";
+      if (!NameHint) {
+        Name = "";
+        Suffix = "";
+      }
+      Suffix += getRefSuffix(Params[I]);
 
-      addInlayHint(Args[I]->getSourceRange(), HintSide::Left,
-                   InlayHintKind::ParameterHint, /*Prefix=*/"", Name,
-                   /*Suffix=*/": ");
+      if (!Name.empty() || !Suffix.empty()) {
+        addInlayHint(Args[I]->getSourceRange(), HintSide::Left,
+                     InlayHintKind::ParameterHint, /*Prefix=*/"", Name, Suffix);
+      }
     }
   }
 
@@ -434,12 +442,21 @@
     return WhatItIsSetting.equals_insensitive(ParamNames[0]);
   }
 
-  bool shouldHint(const Expr *Arg, StringRef ParamName) {
+  StringRef getRefSuffix(const ParmVarDecl *Param) {
+    // If the parameter is a non-const reference type, print an inlay hint
+    auto Type = Param->getType();
+    return Type->isReferenceType() &&
+                   !Type.getNonReferenceType().isConstQualified()
+               ? (Type->isLValueReferenceType() ? "&" : "&&")
+               : "";
+  }
+
+  bool shouldNameHint(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 hint.
+    // parameter name exactly, omit the name hint.
     if (ParamName == getSpelledIdentifier(Arg))
       return false;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124359.424823.patch
Type: text/x-patch
Size: 2385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220425/6bb03116/attachment.bin>


More information about the cfe-commits mailing list