[clangd] Fix a crash in ExtractFunction tweak.
authorHaojian Wu <hokein.wu@gmail.com>
Thu, 13 Oct 2022 08:01:47 +0000 (10:01 +0200)
committerHaojian Wu <hokein.wu@gmail.com>
Thu, 13 Oct 2022 09:12:28 +0000 (11:12 +0200)
Differential Revision: https://reviews.llvm.org/D135857

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

index 4231de8..3b2e7d2 100644 (file)
@@ -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
index 8e1da88..dec63d4 100644 (file)
@@ -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() [[{