Skip to content

Commit aeaeb9e

Browse files
committed
[clangd] Make ExpandAutoType not available on template params.
We cannot expand auto when used inside a template param (C++17 feature), so do not offer it there. Differential Revision: https://reviews.llvm.org/D94719
1 parent 9d2053f commit aeaeb9e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ bool isDeducedAsLambda(const SelectionTree::Node *Node, SourceLocation Loc) {
8282
return false;
8383
}
8484

85+
// Returns true iff "auto" in Node is really part of the template parameter,
86+
// which we cannot expand.
87+
bool isTemplateParam(const SelectionTree::Node *Node) {
88+
if (Node->Parent)
89+
if (Node->Parent->ASTNode.get<NonTypeTemplateParmDecl>())
90+
return true;
91+
return false;
92+
}
93+
8594
bool ExpandAutoType::prepare(const Selection& Inputs) {
8695
CachedLocation = llvm::None;
8796
if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
@@ -90,7 +99,8 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
9099
// Code in apply() does handle 'decltype(auto)' yet.
91100
if (!Result.getTypePtr()->isDecltypeAuto() &&
92101
!isStructuredBindingType(Node) &&
93-
!isDeducedAsLambda(Node, Result.getBeginLoc()))
102+
!isDeducedAsLambda(Node, Result.getBeginLoc()) &&
103+
!isTemplateParam(Node))
94104
CachedLocation = Result;
95105
}
96106
}

clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ TEST_F(ExpandAutoTypeTest, Test) {
8080
// unknown types in a template should not be replaced
8181
EXPECT_THAT(apply("template <typename T> void x() { ^auto y = T::z(); }"),
8282
StartsWith("fail: Could not deduce type for 'auto' type"));
83+
84+
ExtraArgs.push_back("-std=c++17");
85+
EXPECT_UNAVAILABLE("template <au^to X> class Y;");
8386
}
8487

8588
} // namespace

0 commit comments

Comments
 (0)