[clangd] Fix a semantic-highlighting crash.
authorHaojian Wu <hokein.wu@gmail.com>
Mon, 31 Oct 2022 08:58:58 +0000 (09:58 +0100)
committerHaojian Wu <hokein.wu@gmail.com>
Mon, 31 Oct 2022 10:19:01 +0000 (11:19 +0100)
Differential Revision: https://reviews.llvm.org/D137064

clang-tools-extra/clangd/SemanticHighlighting.cpp
clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

index 08cca22..af3a3e6 100644 (file)
@@ -661,7 +661,9 @@ public:
   }
 
   bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE) {
-    if (isa<CXXDestructorDecl>(CE->getMethodDecl())) {
+    // getMethodDecl can return nullptr with member pointers, e.g.
+    // `(foo.*pointer_to_member_fun)(arg);`
+    if (isa_and_present<CXXDestructorDecl>(CE->getMethodDecl())) {
       if (auto *ME = dyn_cast<MemberExpr>(CE->getCallee())) {
         if (auto *TI = ME->getMemberNameInfo().getNamedTypeInfo()) {
           H.addExtraModifier(TI->getTypeLoc().getBeginLoc(),
index 3666f1e..9abc49b 100644 (file)
@@ -887,6 +887,17 @@ sizeof...($TemplateParameter[[Elements]]);
         $TemplateParameter[[T]] $Variable_def[[x]] = {};
         template <>
         int $Variable_def[[x]]<int> = (int)sizeof($Class[[Base]]);
+      )cpp",
+      // no crash
+      R"cpp(
+        struct $Class_def[[Foo]] {
+          void $Method_decl[[foo]]();
+        };
+
+        void $Function_def[[s]]($Class[[Foo]] $Parameter_def[[f]]) {
+          auto $LocalVariable_def[[k]] = &$Class[[Foo]]::$Method[[foo]];
+          ($Parameter[[f]].*$LocalVariable[[k]])(); // no crash on VisitCXXMemberCallExpr
+        }
       )cpp"};
   for (const auto &TestCase : TestCases)
     // Mask off scope modifiers to keep the tests manageable.