1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Test for template specializations.
//
// In template specializations template parameters (e.g. |T| or |T2| in
// MyTemplate below) get substituted with an actual class (e.g. |SomeClass| or
// |int|). In an *implicit* specialization, these substitutions are "overlaid"
// / "overimposed" on top of the template definition and this can lead to
// generating conflicting replacements - for example the same |t_ptr_field|
// definition can get replaced with:
// 1. T* t_ptr_field -> raw_ptr<T> t_ptr_field // expected
// 2. T* t_ptr_field -> raw_ptr<SomeClass> t_ptr_field // undesired
//
// To avoid generating conflicting replacements, the rewriter excludes implicit
// template specializations via |implicit_field_decl_matcher|.
//
// Note that rewrites in *explicit* template specializations are still
// desirable. For example, see the |T2* t2_ptr_field| in |MyTemplate<int, T2>|
// partial template specialization.
#include <optional>
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_span.h"
class SomeClass;
class SomeClass2;
template <typename T, typename T2>
class MyTemplate {
// Expected rewrite: raw_ptr<T> t_ptr_field;
raw_ptr<T> t_ptr_field;
// Expected rewrite: raw_ptr<SomeClass> some_class_ptr_field;
raw_ptr<SomeClass> some_class_ptr_field;
// No rewrite expected.
int int_field;
// Expected rewrite: base::raw_span<T> span_field1;
base::raw_span<T> span_field1;
// Expected rewrite: base::raw_span<SomeClass> span_field2;
base::raw_span<SomeClass> span_field2;
// Expected rewrite: std::optional<base::raw_span<T>> optional_span_field1;
std::optional<base::raw_span<T>> optional_span_field1;
// Expected rewrite: std::optional<base::raw_span<SomeClass>>
// optional_span_field2;
std::optional<base::raw_span<SomeClass>> optional_span_field2;
};
// Partial *explicit* specialization.
template <typename T2>
class MyTemplate<int, T2> {
// Expected rewrite: raw_ptr<T2> t2_ptr_field;
raw_ptr<T2> t2_ptr_field;
// Expected rewrite: raw_ptr<SomeClass> some_class_ptr_field;
raw_ptr<SomeClass> some_class_ptr_field;
// Expected rewrite: raw_ptr<int> int_ptr_field;
raw_ptr<int> int_ptr_field;
// No rewrite expected.
int int_field;
// Expected rewrite: base::raw_span<T2> span_field1;
base::raw_span<T2> span_field1;
// Expected rewrite: base::raw_span<SomeClass> span_field2;
base::raw_span<SomeClass> span_field2;
// Expected rewrite: std::optional<base::raw_span<int>> optional_span_field1;
std::optional<base::raw_span<int>> optional_span_field1;
// Expected rewrite: std::optional<base::raw_span<T2>> optional_span_field2;
std::optional<base::raw_span<T2>> optional_span_field2;
};
// Full *explicit* specialization.
template <>
class MyTemplate<int, SomeClass2> {
// Expected rewrite: raw_ptr<int> int_ptr_field;
raw_ptr<int> int_ptr_field;
// Expected rewrite: raw_ptr<SomeClass2> some_class2_ptr_field;
raw_ptr<SomeClass2> some_class2_ptr_field;
// No rewrite expected.
int int_field;
// Expected rewrite: base::raw_span<T2> span_field1;
base::raw_span<int> span_field1;
// Expected rewrite: base::raw_span<SomeClass> span_field2;
base::raw_span<SomeClass2> span_field2;
// Expected rewrite: std::optional<base::raw_span<int>> optional_span_field1;
std::optional<base::raw_span<int>> optional_span_field1;
// Expected rewrite: std::optional<base::raw_span<T2>> optional_span_field2;
std::optional<base::raw_span<SomeClass2>> optional_span_field2;
};
// The class definitions below trigger an implicit template specialization of
// MyTemplate.
class TemplateDerived : public MyTemplate<SomeClass, int> {};
class TemplateDerived2 : public MyTemplate<SomeClass2, int> {};
// Test where excluding SubstTemplateTypeParmType pointees is not sufficient,
// because the pointee is not |T|, but |TemplateSelfPointerTest<T>| like in
// the fields below.
//
// This test forces using
// classTemplateSpecializationDecl(isImplicitSpecialization())
// in the definition of |implicit_field_decl_matcher|.
// Note that no |hasAncestor| matcher is necessary - compare with
// nested_iterator_test below.
namespace self_pointer_test {
template <typename T>
class TemplateSelfPointerTest {
// Early versions of the rewriter used to rewrite the type below to three
// conflicting replacements:
// 1. raw_ptr<TemplateSelfPointerTest<bool>>
// 2. raw_ptr<TemplateSelfPointerTest<SomeClass2>>
// 3. raw_ptr<TemplateSelfPointerTest<T>>
//
// Something similar would have happened in //base/scoped_generic.h (in the
// nested Receiver class):
// ScopedGeneric* scoped_generic_;
//
// Expected rewrite: raw_ptr<TemplateSelfPointerTest<T>>
raw_ptr<TemplateSelfPointerTest<T>> ptr_field_;
// Similar test to the above. Something similar would have happened in
// //base/id_map.h (in the nested Iterator class):
// IDMap<V, K>* map_;
//
// Expected rewrite: raw_ptr<TemplateSelfPointerTest<T>>
raw_ptr<TemplateSelfPointerTest<T>> ptr_field2_;
// Expected rewrite: base::raw_span<TemplateSelfPointerTest> span_field1;
base::raw_span<TemplateSelfPointerTest> span_field1;
// Expected rewrite: base::raw_span<TemplateSelfPointerTest<T>> span_field2;
base::raw_span<TemplateSelfPointerTest<T>> span_field2;
// Expected rewrite: std::optional<base::raw_span<TemplateSelfPointerTest>>
// optional_span_field1;
std::optional<base::raw_span<TemplateSelfPointerTest>> optional_span_field1;
// Expected rewrite: std::optional<base::raw_span<TemplateSelfPointerTest<T>>>
// optional_span_field2;
std::optional<base::raw_span<TemplateSelfPointerTest<T>>>
optional_span_field2;
};
void foo() {
// Variable declarations below trigger an implicit template specialization of
// TemplateSelfPointerTest.
TemplateSelfPointerTest<bool> foo;
TemplateSelfPointerTest<SomeClass2> bar;
}
} // namespace self_pointer_test
// Test against overlapping replacement that occurred in Chromium in places
// like:
// - //components/url_pattern_index/string_splitter.h
// |const StringSplitter* splitter_| in nested Iterator class
// - //base/callback_list.h
// |CallbackListBase<CallbackType>* list_| in nested Iterator class
// - //mojo/public/cpp/bindings/receiver_set.h
// |ReceiverSetBase* const receiver_set_| in nested Entry class
//
// This test forces using
// hasAncestor(classTemplateSpecializationDecl(isImplicitSpecialization()))
// in the definition of |implicit_field_decl_matcher|.
namespace nested_iterator_test {
template <typename T>
class StringSplitter {
public:
class Iterator {
public:
Iterator(const StringSplitter& splitter) : splitter_(&splitter) {}
private:
// Danger of an overlapping replacement (when substituting
// |StringSplitter<T>| for |StringSplitter<int>| in an implicit template
// specialization triggered by the |foo2| function below.
//
// Expected rewrite: raw_ptr<const StringSplitter<T>> splitter_
raw_ptr<const StringSplitter<T>> splitter_;
// Expected rewrite: base::raw_span<const StringSplitter> span_field
base::raw_span<const StringSplitter> span_field;
};
Iterator begin() const { return Iterator(*this); }
};
void foo2() {
StringSplitter<int> splitter;
auto iterator = splitter.begin();
}
} // namespace nested_iterator_test
// Example based on base/trace_event/memory_usage_estimator.h where a function
// template |EstimateMemoryUsage| had a nested struct |SharedPointer| definition
// with a pointer field |value| that was leading to conflicting replacements.
namespace template_function {
template <typename T>
void foo(T* arg) {
struct NestedStruct {
// Expected rewrite: raw_ptr<T> ptr_field;
raw_ptr<T> ptr_field;
// Expected rewrite: raw_ptr<MyTemplate<T, T>> ptr_field2;
raw_ptr<MyTemplate<T, T>> ptr_field2;
// Expected rewrite: base::raw_span<T> span_field;
base::raw_span<T> span_field;
} var;
var.ptr_field = nullptr;
}
void bar() {
// Triggering implicit specializations of foo that in the past led the
// rewriter to generate conflicting replacements.
int i = 123;
SomeClass* p = nullptr;
foo(p);
foo(&i);
}
} // namespace template_function
|