[clang-tools-extra] 1258747 - [clangd] Fix a semantic-highlighting crash.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 31 03:19:22 PDT 2022


Author: Haojian Wu
Date: 2022-10-31T11:19:01+01:00
New Revision: 1258747a59db5200112fca7c6140d184f3b8748e

URL: https://github.com/llvm/llvm-project/commit/1258747a59db5200112fca7c6140d184f3b8748e
DIFF: https://github.com/llvm/llvm-project/commit/1258747a59db5200112fca7c6140d184f3b8748e.diff

LOG: [clangd] Fix a semantic-highlighting crash.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp
index 08cca2203667b..af3a3e6f8e941 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -661,7 +661,9 @@ class CollectExtraHighlightings
   }
 
   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(),

diff  --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
index 3666f1e5f21bc..9abc49bb06014 100644
--- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -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.


        


More information about the cfe-commits mailing list