Skip to content

Commit

Permalink
[clangd] Fix an assertion crash in "ExtractVariable" tweak
Browse files Browse the repository at this point in the history
Summary:
GetTypePtr requires that the type should not be null, otherwise we hit
an assertion, we should use getTypePtrOrNull instead.

Reviewers: sammccall, SureYeaah

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

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

llvm-svn: 365763
  • Loading branch information
hokein committed Jul 11, 2019
1 parent badece0 commit 1503a3b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
11 changes: 4 additions & 7 deletions clang-tools-extra/clangd/Selection.cpp
Expand Up @@ -358,13 +358,10 @@ SelectionTree::SelectionTree(ASTContext &AST, unsigned Offset)
const Node *SelectionTree::commonAncestor() const {
if (!Root)
return nullptr;
for (const Node *Ancestor = Root;; Ancestor = Ancestor->Children.front()) {
if (Ancestor->Selected || Ancestor->Children.size() > 1)
return Ancestor;
// The tree only contains ancestors of the interesting nodes.
assert(!Ancestor->Children.empty() && "bad node in selection tree");
}
return nullptr;
const Node *Ancestor = Root;
while (Ancestor->Children.size() == 1 && !Ancestor->Selected)
Ancestor = Ancestor->Children.front();
return Ancestor;
}

} // namespace clangd
Expand Down
Expand Up @@ -81,7 +81,7 @@ computeReferencedDecls(const clang::Expr *Expr) {
// FIXME: Ignore assignment (a = 1) Expr since it is extracted as dummy = a =
static bool isExtractableExpr(const clang::Expr *Expr) {
if (Expr) {
const Type *ExprType = Expr->getType().getTypePtr();
const Type *ExprType = Expr->getType().getTypePtrOrNull();
// FIXME: check if we need to cover any other types
if (ExprType)
return !ExprType->isVoidType();
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/unittests/TweakTests.cpp
Expand Up @@ -313,6 +313,14 @@ TEST(TweakTest, ExtractVariable) {
while(a < ^3);
}
)cpp");
// Should not crash.
checkNotAvailable(ID, R"cpp(
template<typename T, typename ...Args>
struct Test<T, Args...> {
Test(const T &v) :val(^) {}
T val;
};
)cpp");
checkNotAvailable(ID, R"cpp(
int xyz(int a = ^1) {
return 1;
Expand Down

0 comments on commit 1503a3b

Please sign in to comment.