[clang-tools-extra] e37653d - [clangd] Type hints for C++14 return type deduction

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 20 22:13:11 PDT 2021


Author: Nathan Ridge
Date: 2021-06-21T01:13:00-04:00
New Revision: e37653da1399d846e02897680412139fdcde93ab

URL: https://github.com/llvm/llvm-project/commit/e37653da1399d846e02897680412139fdcde93ab
DIFF: https://github.com/llvm/llvm-project/commit/e37653da1399d846e02897680412139fdcde93ab.diff

LOG: [clangd] Type hints for C++14 return type deduction

Differential Revision: https://reviews.llvm.org/D103789

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index d0e0e961abcf3..1002aee218477 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -72,6 +72,19 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
     return true;
   }
 
+  bool VisitFunctionDecl(FunctionDecl *D) {
+    if (auto *AT = D->getReturnType()->getContainedAutoType()) {
+      QualType Deduced = AT->getDeducedType();
+      if (!Deduced.isNull()) {
+        addInlayHint(D->getFunctionTypeLoc().getRParenLoc(),
+                     InlayHintKind::TypeHint,
+                     "-> " + D->getReturnType().getAsString(TypeHintPolicy));
+      }
+    }
+
+    return true;
+  }
+
   bool VisitVarDecl(VarDecl *D) {
     // Do not show hints for the aggregate in a structured binding.
     // In the future, we may show hints for the individual bindings.

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 3c5503448ab8f..2c5597e17e2f0 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -478,14 +478,31 @@ TEST(TypeHints, StructuredBindings) {
 }
 
 TEST(TypeHints, ReturnTypeDeduction) {
-  // FIXME: Not handled yet.
-  // This test is currently here mostly because a naive implementation
-  // might have us print something not super helpful like the function type.
-  assertTypeHints(R"cpp(
-    auto func(int x) {
-      return x + 1;
-    }
-  )cpp");
+  assertTypeHints(
+      R"cpp(
+    auto f1(int x$ret1a[[)]];  // Hint forward declaration too
+    auto f1(int x$ret1b[[)]] { return x + 1; }
+
+    // Include pointer operators in hint
+    int s;
+    auto& f2($ret2[[)]] { return s; }
+
+    // Do not hint `auto` for trailing return type.
+    auto f3() -> int;
+
+    // `auto` conversion operator
+    struct A {
+      operator auto($retConv[[)]] { return 42; }
+    };
+
+    // FIXME: Dependent types do not work yet.
+    template <typename T>
+    struct S {
+      auto method() { return T(); }
+    };
+  )cpp",
+      ExpectedHint{"-> int", "ret1a"}, ExpectedHint{"-> int", "ret1b"},
+      ExpectedHint{"-> int &", "ret2"}, ExpectedHint{"-> int", "retConv"});
 }
 
 TEST(TypeHints, DependentType) {


        


More information about the cfe-commits mailing list