[clang-tools-extra] 7ac10e5 - [clangd] Fix a crash in ExtractFunction tweak.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 13 02:13:14 PDT 2022


Author: Haojian Wu
Date: 2022-10-13T11:12:28+02:00
New Revision: 7ac10e5b347f3e22ae1dad5937de667e57c472be

URL: https://github.com/llvm/llvm-project/commit/7ac10e5b347f3e22ae1dad5937de667e57c472be
DIFF: https://github.com/llvm/llvm-project/commit/7ac10e5b347f3e22ae1dad5937de667e57c472be.diff

LOG: [clangd] Fix a crash in ExtractFunction tweak.

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
    clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
index 4231de8cc45d6..3b2e7d2bb86a7 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
@@ -248,6 +248,8 @@ const FunctionDecl *findEnclosingFunction(const Node *CommonAnc) {
       // FIXME: Support extraction from templated functions.
       if (Func->isTemplated())
         return nullptr;
+      if (!Func->getBody())
+        return nullptr;
       for (const auto *S : Func->getBody()->children()) {
         // During apply phase, we perform semantic analysis (e.g. figure out
         // what variables requires hoisting). We cannot perform those when the

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
index 8e1da884bbd7d..dec63d454d52c 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
@@ -182,6 +182,7 @@ F (extracted();)
 
   // Shouldn't crash.
   EXPECT_EQ(apply("void f([[int a]]);"), "unavailable");
+  EXPECT_EQ(apply("void f(int a = [[1]]);"), "unavailable");
   // Don't extract if we select the entire function body (CompoundStmt).
   std::string CompoundFailInput = R"cpp(
     void f() [[{


        


More information about the cfe-commits mailing list