[PATCH] D73723: [clangd][Hover] Handle uninstantiated default args
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 31 06:02:53 PST 2020
kadircet updated this revision to Diff 241707.
kadircet marked an inline comment as done.
kadircet added a comment.
- Address comments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D73723/new/
https://reviews.llvm.org/D73723
Files:
clang-tools-extra/clangd/Hover.cpp
clang-tools-extra/clangd/unittests/HoverTests.cpp
Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -1609,6 +1609,22 @@
HI.Type = "unsigned long";
HI.Value = "1";
}},
+ {
+ R"cpp(
+ template <typename T = int>
+ void foo(const T& = T()) {
+ [[f^oo]]<>(3);
+ })cpp",
+ [](HoverInfo &HI) {
+ HI.Name = "foo";
+ HI.Kind = index::SymbolKind::Function;
+ HI.Type = "void (const int &)";
+ HI.ReturnType = "void";
+ HI.Parameters = {
+ {std::string("const int &"), llvm::None, std::string("T()")}};
+ HI.Definition = "template <> void foo<int>(const int &)";
+ HI.NamespaceScope = "";
+ }},
};
// Create a tiny index, so tests above can verify documentation is fetched.
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -250,6 +250,20 @@
});
}
+// Default argument might exist but be unavailable, in the case of unparsed
+// arguments for example. This function returns the default argument if it is
+// available.
+const Expr *getDefaultArg(const ParmVarDecl *PVD) {
+ // Default argument can be unparsed or uninstatiated. For the former we
+ // can't do much, as token information is only stored in Sema and not
+ // attached to the AST node. For the latter though, it is safe to proceed as
+ // the expression is still valid.
+ if (!PVD->hasDefaultArg() || PVD->hasUnparsedDefaultArg())
+ return nullptr;
+ return PVD->hasUninstantiatedDefaultArg() ? PVD->getUninstantiatedDefaultArg()
+ : PVD->getDefaultArg();
+}
+
// Populates Type, ReturnType, and Parameters for function-like decls.
void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
const FunctionDecl *FD,
@@ -269,10 +283,10 @@
}
if (!PVD->getName().empty())
P.Name = PVD->getNameAsString();
- if (PVD->hasDefaultArg()) {
+ if (const Expr *DefArg = getDefaultArg(PVD)) {
P.Default.emplace();
llvm::raw_string_ostream Out(*P.Default);
- PVD->getDefaultArg()->printPretty(Out, nullptr, Policy);
+ DefArg->printPretty(Out, nullptr, Policy);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73723.241707.patch
Type: text/x-patch
Size: 2539 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200131/d56c0166/attachment-0001.bin>
More information about the cfe-commits
mailing list