[clang-tools-extra] 2174524 - [clangd] AddUsing: Fix support for template specializations.
Adam Czachorowski via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 26 08:35:23 PDT 2021
Author: Adam Czachorowski
Date: 2021-10-26T17:32:33+02:00
New Revision: 2174524116a8379fb7a6453253524ec972b158df
URL: https://github.com/llvm/llvm-project/commit/2174524116a8379fb7a6453253524ec972b158df
DIFF: https://github.com/llvm/llvm-project/commit/2174524116a8379fb7a6453253524ec972b158df.diff
LOG: [clangd] AddUsing: Fix support for template specializations.
Before this change, we would add "using std::vector<int>" instead of
just "using std::vector;", which would not even compile.
Fixes https://github.com/clangd/clangd/issues/904
Differential Revision: https://reviews.llvm.org/D112530
Added:
Modified:
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index a8c937a951df..c1adbdde6199 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -278,8 +278,13 @@ bool AddUsing::prepare(const Selection &Inputs) {
if (!QualifierToRemove)
return false;
- auto SpelledTokens =
- TB.spelledForExpanded(TB.expandedTokens(E.getSourceRange()));
+ auto NameRange = E.getSourceRange();
+ if (auto T = E.getNamedTypeLoc().getAs<TemplateSpecializationTypeLoc>()) {
+ // Remove the template arguments from the name.
+ NameRange.setEnd(T.getLAngleLoc().getLocWithOffset(-1));
+ }
+
+ auto SpelledTokens = TB.spelledForExpanded(TB.expandedTokens(NameRange));
if (!SpelledTokens)
return false;
auto SpelledRange = syntax::Token::range(SM, SpelledTokens->front(),
diff --git a/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
index c0c66dbc14d7..b0c2d71328bd 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/AddUsingTests.cpp
@@ -444,6 +444,17 @@ one::t^wo::cc c;
#include "test.hpp"
using one::two::cc;using one::two::ee::ee_one;
cc c;
+)cpp"},
+ // Template (like std::vector).
+ {R"cpp(
+#include "test.hpp"
+one::v^ec<int> foo;
+)cpp",
+ R"cpp(
+#include "test.hpp"
+using one::vec;
+
+vec<int> foo;
)cpp"}};
llvm::StringMap<std::string> EditedFiles;
for (const auto &Case : Cases) {
@@ -461,6 +472,7 @@ class cc {
};
}
using uu = two::cc;
+template<typename T> struct vec {};
})cpp";
EXPECT_EQ(apply(SubCase, &EditedFiles), Case.ExpectedSource);
}
More information about the cfe-commits
mailing list