[clang-tools-extra] r365763 - [clangd] Fix an assertion crash in "ExtractVariable" tweak

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 11 05:29:01 PDT 2019


Author: hokein
Date: Thu Jul 11 05:29:01 2019
New Revision: 365763

URL: http://llvm.org/viewvc/llvm-project?rev=365763&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clangd/Selection.cpp
    clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
    clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/Selection.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Selection.cpp?rev=365763&r1=365762&r2=365763&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Selection.cpp (original)
+++ clang-tools-extra/trunk/clangd/Selection.cpp Thu Jul 11 05:29:01 2019
@@ -358,13 +358,10 @@ SelectionTree::SelectionTree(ASTContext
 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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=365763&r1=365762&r2=365763&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Thu Jul 11 05:29:01 2019
@@ -81,7 +81,7 @@ computeReferencedDecls(const clang::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();

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=365763&r1=365762&r2=365763&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Thu Jul 11 05:29:01 2019
@@ -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;




More information about the cfe-commits mailing list