[PATCH] D103789: [clangd] Type hints for C++14 return type deduction
Nathan Ridge via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 6 23:54:02 PDT 2021
nridge created this revision.
nridge added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
nridge requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D103789
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
@@ -478,14 +478,32 @@
}
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(
+ $ret1a[[auto]] f1(int x); // Hint forward declaration too
+ $ret1b[[auto]] f1(int x) { return x + 1; }
+
+ // Include pointer operators in both location and hint
+ // (we could also go the other way).
+ int s;
+ $ret2[[auto&]] f2() { return s; }
+
+ // Do not hint `auto` for trailing return type.
+ auto f3() -> int;
+
+ // `auto` conversion operator
+ struct A {
+ operator $retConv[[auto]]() { 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) {
Index: clang-tools-extra/clangd/InlayHints.cpp
===================================================================
--- clang-tools-extra/clangd/InlayHints.cpp
+++ clang-tools-extra/clangd/InlayHints.cpp
@@ -72,6 +72,23 @@
return true;
}
+ bool VisitFunctionDecl(FunctionDecl *D) {
+ if (auto *AT = D->getReturnType()->getContainedAutoType()) {
+ QualType Deduced = AT->getDeducedType();
+ if (!Deduced.isNull()) {
+ SourceRange R = D->getReturnTypeSourceRange();
+ // For operator auto(), have to get location of `auto` a different way.
+ if (R.isInvalid() && isa<CXXConversionDecl>(D)) {
+ R = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
+ }
+ addInlayHint(R, 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103789.350182.patch
Type: text/x-patch
Size: 2461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210607/146a37fd/attachment-0001.bin>
More information about the cfe-commits
mailing list