[clangd] Fix a crash in expected types
authorIlya Biryukov <ibiryukov@google.com>
Wed, 20 Nov 2019 14:51:18 +0000 (15:51 +0100)
committerIlya Biryukov <ibiryukov@google.com>
Wed, 20 Nov 2019 15:40:28 +0000 (16:40 +0100)
Reviewers: kadircet

Reviewed By: kadircet

Subscribers: merge_guards_bot, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

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

clang-tools-extra/clangd/ExpectedTypes.cpp
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

index 3b0779ea66bc6d9546784b0f38b8d5d572f210bc..a82a64cf14e2bbaa1cf85f0475bd38de8289fbbf 100644 (file)
@@ -44,12 +44,10 @@ static const Type *toEquivClass(ASTContext &Ctx, QualType T) {
 static llvm::Optional<QualType>
 typeOfCompletion(const CodeCompletionResult &R) {
   const NamedDecl *D = R.Declaration;
-  if (!D)
-    return llvm::None;
   // Templates do not have a type on their own, look at the templated decl.
-  if (auto *Template = dyn_cast<TemplateDecl>(D))
+  if (auto *Template = dyn_cast_or_null<TemplateDecl>(D))
     D = Template->getTemplatedDecl();
-  auto *VD = dyn_cast<ValueDecl>(D);
+  auto *VD = dyn_cast_or_null<ValueDecl>(D);
   if (!VD)
     return llvm::None; // We handle only variables and functions below.
   auto T = VD->getType();
index 5b50b9fe9f8ba5606564628e1d9790efd88fec71..e69b2a6205f6a8ea8d95496834e31c2704f3ec58 100644 (file)
@@ -1030,6 +1030,16 @@ TEST(CompletionTest, DefaultArgs) {
                         SnippetSuffix("(${1:int A})"))));
 }
 
+TEST(CompletionTest, NoCrashWithTemplateParamsAndPreferredTypes) {
+  auto Completions = completions(R"cpp(
+template <template <class> class TT> int foo() {
+  int a = ^
+}
+)cpp")
+                         .Completions;
+  EXPECT_THAT(Completions, Contains(Named("TT")));
+}
+
 SignatureHelp signatures(llvm::StringRef Text, Position Point,
                          std::vector<Symbol> IndexSymbols = {}) {
   std::unique_ptr<SymbolIndex> Index;