[PATCH] D77656: [clangd] Fix a crash bug in AddUsing tweak around template handling.

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 7 09:12:30 PDT 2020


adamcz created this revision.
adamcz added a reviewer: sammccall.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.
adamcz updated this revision to Diff 255694.
adamcz added a comment.

Removed unnecessary change.


The crash happened on cases like:
template<typename TT> using one = two::three<T^T>;
because we tried to call getName() on getBaseTypeIdentifier(), which can
be nullptr.

Ideally we would support this use case as well, but for now not crashing
will do.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77656

Files:
  clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
  clang-tools-extra/clangd/unittests/TweakTests.cpp


Index: clang-tools-extra/clangd/unittests/TweakTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -2436,6 +2436,7 @@
 #define NS(name) one::two::name
 namespace one {
 void oo() {}
+template<typename TT> class tt {};
 namespace two {
 enum ee {};
 void ff() {}
@@ -2458,6 +2459,10 @@
   EXPECT_UNAVAILABLE(Header +
                      "void fun() { o^n^e^:^:^t^w^o^:^:^c^c^:^:^s^t inst; }");
   EXPECT_UNAVAILABLE(Header + "void fun() { N^S(c^c) inst; }");
+  // This used to crash. Ideally we would support this case, but for now we just
+  // test that we don't crash.
+  EXPECT_UNAVAILABLE(Header +
+                     "template<typename TT> using foo = one::tt<T^T>;");
 }
 
 TEST_F(AddUsingTest, Apply) {
Index: clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -206,9 +206,12 @@
     Name = D->getDecl()->getName();
   } else if (auto *T = Node->ASTNode.get<TypeLoc>()) {
     if (auto E = T->getAs<ElaboratedTypeLoc>()) {
-      QualifierToRemove = E.getQualifierLoc();
-      Name =
-          E.getType().getUnqualifiedType().getBaseTypeIdentifier()->getName();
+      if (auto *BaseTypeIdentifier =
+              E.getType().getUnqualifiedType().getBaseTypeIdentifier()) {
+        Name =
+            E.getType().getUnqualifiedType().getBaseTypeIdentifier()->getName();
+        QualifierToRemove = E.getQualifierLoc();
+      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77656.255694.patch
Type: text/x-patch
Size: 1688 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200407/7e80e821/attachment-0001.bin>


More information about the cfe-commits mailing list