Skip to content

Commit

Permalink
[CodeComplete] [clangd] Fix crash on ValueDecl with a null type
Browse files Browse the repository at this point in the history
Reviewers: kadircet

Reviewed By: kadircet

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

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

llvm-svn: 352040
  • Loading branch information
ilya-biryukov committed Jan 24, 2019
1 parent fa2e927 commit c514ade
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion clang-tools-extra/clangd/ExpectedTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CodeCompletion/crash-null-type.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c514ade

Please sign in to comment.