From: Kadir Cetinkaya Date: Wed, 5 Aug 2020 14:21:55 +0000 (+0200) Subject: [clangd] Disable define out-of-line code action on templates X-Git-Tag: llvmorg-13-init~15659 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d3ac30188666b182daa87d23533543543b789e97;p=platform%2Fupstream%2Fllvm.git [clangd] Disable define out-of-line code action on templates Differential Revision: https://reviews.llvm.org/D85310 --- diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp index d37d377..9f68e24 100644 --- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp @@ -122,9 +122,8 @@ getFunctionSourceAfterReplacements(const FunctionDecl *FD, if (!OrigFuncRange) return llvm::createStringError(llvm::inconvertibleErrorCode(), "Couldn't get range for function."); - // Include template parameter list. - if (auto *FTD = FD->getDescribedFunctionTemplate()) - OrigFuncRange->setBegin(FTD->getBeginLoc()); + assert(!FD->getDescribedFunctionTemplate() && + "Define out-of-line doesn't apply to function templates."); // Get new begin and end positions for the qualified function definition. unsigned FuncBegin = SM.getFileOffset(OrigFuncRange->getBegin()); @@ -387,6 +386,13 @@ public: Source->isOutOfLine()) return false; + // Bail out if this is a function template or specialization, as their + // definitions need to be visible in all including translation units. + if (auto *PT = Source->getDescribedFunctionTemplate()) + return false; + if (auto *TSI = Source->getTemplateSpecializationInfo()) + return false; + // Bail out in templated classes, as it is hard to spell the class name, i.e // if the template parameter is unnamed. if (auto *MD = llvm::dyn_cast(Source)) { diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp index 7919651..200a53c 100644 --- a/clang-tools-extra/clangd/unittests/TweakTests.cpp +++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp @@ -2008,6 +2008,13 @@ TEST_F(DefineOutlineTest, TriggersOnFunctionDecl) { EXPECT_UNAVAILABLE(R"cpp( template struct Foo { void fo^o(){} }; )cpp"); + + // Not available on function templates and specializations, as definition must + // be visible to all translation units. + EXPECT_UNAVAILABLE(R"cpp( + template void fo^o() {}; + template <> void fo^o() {}; + )cpp"); } TEST_F(DefineOutlineTest, FailsWithoutSource) { @@ -2037,27 +2044,6 @@ TEST_F(DefineOutlineTest, ApplyTest) { "void foo() ;", "void foo() { return; }", }, - // Templated function. - { - "template void fo^o(T, T x) { return; }", - "template void foo(T, T x) ;", - "template void foo(T, T x) { return; }", - }, - { - "template void fo^o() { return; }", - "template void foo() ;", - "template void foo() { return; }", - }, - // Template specialization. - { - R"cpp( - template void foo(); - template <> void fo^o() { return; })cpp", - R"cpp( - template void foo(); - template <> void foo() ;)cpp", - "template <> void foo() { return; }", - }, // Default args. { "void fo^o(int x, int y = 5, int = 2, int (*foo)(int) = nullptr) {}",