[CodeComplete] [clangd] Fix crash on ValueDecl with a null type

Reviewers: kadircet

Reviewed By: kadircet

Subscribers: ioeric, MaskRay, jkorous, arphaman, cfe-commits

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

llvm-svn: 352040
This commit is contained in:
Ilya Biryukov 2019-01-24 10:41:43 +00:00
parent fa2e927c44
commit c514adef05
4 changed files with 24 additions and 2 deletions

View File

@ -35,8 +35,10 @@ static llvm::Optional<QualType>
typeOfCompletion(const CodeCompletionResult &R) {
auto *VD = dyn_cast_or_null<ValueDecl>(R.Declaration);
if (!VD)
return None; // We handle only variables and functions below.
return llvm::None; // We handle only variables and functions below.
auto T = VD->getType();
if (T.isNull())
return llvm::None;
if (auto FuncT = T->getAs<FunctionType>()) {
// Functions are a special case. They are completed as 'foo()' and we want
// to match their return type rather than the function type itself.

View File

@ -2319,6 +2319,17 @@ TEST(CompletionTest, ObjectiveCMethodTwoArgumentsFromMiddle) {
EXPECT_THAT(C, ElementsAre(SnippetSuffix("${1:(unsigned int)}")));
}
TEST(CompletionTest, WorksWithNullType) {
auto R = completions(R"cpp(
int main() {
for (auto [loopVar] : y ) { // y has to be unresolved.
int z = loopV^;
}
}
)cpp");
EXPECT_THAT(R.Completions, ElementsAre(Named("loopVar")));
}
} // namespace
} // namespace clangd
} // namespace clang

View File

@ -680,7 +680,8 @@ QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) {
T = Property->getType();
else if (const auto *Value = dyn_cast<ValueDecl>(ND))
T = Value->getType();
else
if (T.isNull())
return QualType();
// Dig through references, function pointers, and block pointers to

View File

@ -0,0 +1,8 @@
void test() {
for (auto [loopVar] : y) { // y has to be unresolved
loopVa
}
}
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:3:11 %s -o - \
// RUN: | FileCheck %s
// CHECK: COMPLETION: loopVar