Skip to content

Commit d2ac21d

Browse files
authored
[RecursiveASTVisitor] Assert we skip implicit instantiations. (llvm#110899)
In DEF_TRAVERSE_TMPL_SPEC_DECL, we attempt to skip implicit instantiations by detecting that D->getTemplateArgsAsWritten() returns nullptr. Previously, this was not reliable. To ensure we do not regress, add an assertion and a test.
1 parent 077a796 commit d2ac21d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

clang-tools-extra/test/clang-tidy/checkers/modernize/type-traits.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@ namespace std {
1414
static constexpr bool value = true;
1515
};
1616

17+
template <typename T, typename U>
18+
static constexpr bool is_same_v = is_same<T, U>::value; // NOLINT
19+
1720
template<bool, typename T = void>
1821
struct enable_if {
1922
using type = T;
2023
};
2124

25+
template <bool B, typename T = void>
26+
using enable_if_t = typename enable_if<B, T>::type; // NOLINT
27+
28+
template <typename T>
29+
struct remove_reference {
30+
using type = T;
31+
};
32+
33+
template <typename T>
34+
using remove_reference_t = typename remove_reference<T>::type; // NOLINT
35+
2236
template <typename...>
2337
struct common_type {
2438
using type = int;
@@ -126,3 +140,13 @@ namespace my_std = std;
126140
using Alias = my_std::add_const<bool>::type;
127141
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use c++14 style type templates
128142
// CHECK-FIXES: using Alias = my_std::add_const_t<bool>;
143+
144+
template <typename T>
145+
struct ImplicitlyInstantiatedConstructor {
146+
template <typename U, typename = std::enable_if_t<std::is_same_v<U, T>>>
147+
ImplicitlyInstantiatedConstructor(U) {}
148+
};
149+
150+
const ImplicitlyInstantiatedConstructor<int> ImplicitInstantiation(std::remove_reference<int>::type(123));
151+
// CHECK-MESSAGES: :[[@LINE-1]]:68: warning: use c++14 style type templates
152+
// CHECK-FIXES: const ImplicitlyInstantiatedConstructor<int> ImplicitInstantiation(std::remove_reference_t<int>(123));

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,7 @@ bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper(
21942194
is the only callback that's made for this instantiation. \
21952195
We use getTemplateArgsAsWritten() to distinguish. */ \
21962196
if (const auto *ArgsWritten = D->getTemplateArgsAsWritten()) { \
2197+
assert(D->getTemplateSpecializationKind() != TSK_ImplicitInstantiation); \
21972198
/* The args that remains unspecialized. */ \
21982199
TRY_TO(TraverseTemplateArgumentLocsHelper( \
21992200
ArgsWritten->getTemplateArgs(), ArgsWritten->NumTemplateArgs)); \

0 commit comments

Comments
 (0)