diff options
author | Haojian Wu <hokein@google.com> | 2019-07-11 12:29:01 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2019-07-11 12:29:01 +0000 |
commit | 1503a3b2e714e8ea6f4d201b947d88241a92a648 (patch) | |
tree | b8487e80a5f6e2637eed0120eb828258e3355b39 | |
parent | badece02b40d5c21d0c6c1cf209631e5fb19d9c0 (diff) | |
download | bcm5719-llvm-1503a3b2e714e8ea6f4d201b947d88241a92a648.tar.gz bcm5719-llvm-1503a3b2e714e8ea6f4d201b947d88241a92a648.zip |
[clangd] Fix an assertion crash in "ExtractVariable" tweak
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
-rw-r--r-- | clang-tools-extra/clangd/Selection.cpp | 11 | ||||
-rw-r--r-- | clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp | 2 | ||||
-rw-r--r-- | clang-tools-extra/clangd/unittests/TweakTests.cpp | 8 |
3 files changed, 13 insertions, 8 deletions
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp index 9c2fb27a626..7e41de02758 100644 --- a/clang-tools-extra/clangd/Selection.cpp +++ b/clang-tools-extra/clangd/Selection.cpp @@ -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 diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp index 18f71fa0156..447110d3e90 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp @@ -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(); diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp index 15ffe935522..652afb0cec4 100644 --- a/clang-tools-extra/clangd/unittests/TweakTests.cpp +++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp @@ -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; |