blob: 2c22a5cf9ed9866cba8fc22f86411246e2494fd9 [file] [log] [blame]
Shuai Wange9192f82018-09-11 21:13:20 +00001//===---------- ExprMutationAnalyzerTest.cpp ------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shuai Wange9192f82018-09-11 21:13:20 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Tooling/Tooling.h"
Alexander Kornienko973fcc22019-01-08 16:55:13 +000013#include "llvm/ADT/SmallString.h"
Shuai Wange9192f82018-09-11 21:13:20 +000014#include "gmock/gmock.h"
15#include "gtest/gtest.h"
16#include <cctype>
17
18namespace clang {
19
20using namespace clang::ast_matchers;
21using ::testing::ElementsAre;
22using ::testing::IsEmpty;
23using ::testing::ResultOf;
24using ::testing::StartsWith;
25using ::testing::Values;
26
27namespace {
28
29using ExprMatcher = internal::Matcher<Expr>;
30using StmtMatcher = internal::Matcher<Stmt>;
31
Shuai Wangb81bcb32018-09-19 03:50:03 +000032std::unique_ptr<ASTUnit>
33buildASTFromCodeWithArgs(const Twine &Code,
34 const std::vector<std::string> &Args) {
Alexander Kornienko973fcc22019-01-08 16:55:13 +000035 SmallString<1024> CodeStorage;
36 auto AST =
37 tooling::buildASTFromCodeWithArgs(Code.toStringRef(CodeStorage), Args);
Shuai Wangb81bcb32018-09-19 03:50:03 +000038 EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
39 return AST;
40}
41
42std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code) {
43 return buildASTFromCodeWithArgs(Code, {});
44}
45
Shuai Wange9192f82018-09-11 21:13:20 +000046ExprMatcher declRefTo(StringRef Name) {
47 return declRefExpr(to(namedDecl(hasName(Name))));
48}
49
50StmtMatcher withEnclosingCompound(ExprMatcher Matcher) {
51 return expr(Matcher, hasAncestor(compoundStmt().bind("stmt"))).bind("expr");
52}
53
54bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
55 const auto *const S = selectFirst<Stmt>("stmt", Results);
56 const auto *const E = selectFirst<Expr>("expr", Results);
57 return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(E);
58}
59
60SmallVector<std::string, 1>
61mutatedBy(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
62 const auto *const S = selectFirst<Stmt>("stmt", Results);
63 SmallVector<std::string, 1> Chain;
64 ExprMutationAnalyzer Analyzer(*S, AST->getASTContext());
65 for (const auto *E = selectFirst<Expr>("expr", Results); E != nullptr;) {
66 const Stmt *By = Analyzer.findMutation(E);
67 std::string buffer;
68 llvm::raw_string_ostream stream(buffer);
69 By->printPretty(stream, nullptr, AST->getASTContext().getPrintingPolicy());
70 Chain.push_back(StringRef(stream.str()).trim().str());
71 E = dyn_cast<DeclRefExpr>(By);
72 }
73 return Chain;
74}
75
76std::string removeSpace(std::string s) {
77 s.erase(std::remove_if(s.begin(), s.end(),
78 [](char c) { return std::isspace(c); }),
79 s.end());
80 return s;
81}
82
Shuai Wang43059932018-09-17 20:10:56 +000083const std::string StdRemoveReference =
84 "namespace std {"
85 "template<class T> struct remove_reference { typedef T type; };"
86 "template<class T> struct remove_reference<T&> { typedef T type; };"
87 "template<class T> struct remove_reference<T&&> { typedef T type; }; }";
88
89const std::string StdMove =
90 "namespace std {"
91 "template<class T> typename remove_reference<T>::type&& "
92 "move(T&& t) noexcept {"
93 "return static_cast<typename remove_reference<T>::type&&>(t); } }";
94
95const std::string StdForward =
96 "namespace std {"
97 "template<class T> T&& "
98 "forward(typename remove_reference<T>::type& t) noexcept { return t; }"
99 "template<class T> T&& "
Shuai Wangb81bcb32018-09-19 03:50:03 +0000100 "forward(typename remove_reference<T>::type&& t) noexcept { return t; } }";
Shuai Wang43059932018-09-17 20:10:56 +0000101
Shuai Wange9192f82018-09-11 21:13:20 +0000102} // namespace
103
104TEST(ExprMutationAnalyzerTest, Trivial) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000105 const auto AST = buildASTFromCode("void f() { int x; x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000106 const auto Results =
107 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
108 EXPECT_FALSE(isMutated(Results, AST.get()));
109}
110
111class AssignmentTest : public ::testing::TestWithParam<std::string> {};
112
113TEST_P(AssignmentTest, AssignmentModifies) {
114 const std::string ModExpr = "x " + GetParam() + " 10";
Shuai Wangb81bcb32018-09-19 03:50:03 +0000115 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000116 const auto Results =
117 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
118 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
119}
120
121INSTANTIATE_TEST_CASE_P(AllAssignmentOperators, AssignmentTest,
122 Values("=", "+=", "-=", "*=", "/=", "%=", "&=", "|=",
123 "^=", "<<=", ">>="), );
124
125class IncDecTest : public ::testing::TestWithParam<std::string> {};
126
127TEST_P(IncDecTest, IncDecModifies) {
128 const std::string ModExpr = GetParam();
Shuai Wangb81bcb32018-09-19 03:50:03 +0000129 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000130 const auto Results =
131 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
132 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
133}
134
135INSTANTIATE_TEST_CASE_P(AllIncDecOperators, IncDecTest,
136 Values("++x", "--x", "x++", "x--"), );
137
138TEST(ExprMutationAnalyzerTest, NonConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000139 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000140 "void f() { struct Foo { void mf(); }; Foo x; x.mf(); }");
141 const auto Results =
142 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
143 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
144}
145
146TEST(ExprMutationAnalyzerTest, AssumedNonConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000147 auto AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000148 "struct X { template <class T> void mf(); };"
149 "template <class T> void f() { X x; x.mf<T>(); }",
150 {"-fno-delayed-template-parsing"});
151 auto Results =
152 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
153 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf<T>()"));
154
Shuai Wangb81bcb32018-09-19 03:50:03 +0000155 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.mf(); }",
156 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000157 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
158 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
159
Shuai Wangb81bcb32018-09-19 03:50:03 +0000160 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000161 "template <class T> struct X;"
162 "template <class T> void f() { X<T> x; x.mf(); }",
163 {"-fno-delayed-template-parsing"});
164 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
165 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
166}
167
168TEST(ExprMutationAnalyzerTest, ConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000169 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000170 "void f() { struct Foo { void mf() const; }; Foo x; x.mf(); }");
171 const auto Results =
172 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
173 EXPECT_FALSE(isMutated(Results, AST.get()));
174}
175
176TEST(ExprMutationAnalyzerTest, NonConstOperator) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000177 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000178 "void f() { struct Foo { Foo& operator=(int); }; Foo x; x = 10; }");
179 const auto Results =
180 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
181 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x = 10"));
182}
183
184TEST(ExprMutationAnalyzerTest, ConstOperator) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000185 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000186 "void f() { struct Foo { int operator()() const; }; Foo x; x(); }");
187 const auto Results =
188 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
189 EXPECT_FALSE(isMutated(Results, AST.get()));
190}
191
192TEST(ExprMutationAnalyzerTest, ByValueArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000193 auto AST = buildASTFromCode("void g(int); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000194 auto Results =
195 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
196 EXPECT_FALSE(isMutated(Results, AST.get()));
197
Shuai Wangb81bcb32018-09-19 03:50:03 +0000198 AST = buildASTFromCode("void g(int*); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000199 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
200 EXPECT_FALSE(isMutated(Results, AST.get()));
201
Shuai Wangb81bcb32018-09-19 03:50:03 +0000202 AST = buildASTFromCode("typedef int* IntPtr;"
203 "void g(IntPtr); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000204 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
205 EXPECT_FALSE(isMutated(Results, AST.get()));
206
Shuai Wangb81bcb32018-09-19 03:50:03 +0000207 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000208 "struct A {}; A operator+(A, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000209 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
210 EXPECT_FALSE(isMutated(Results, AST.get()));
211
Shuai Wangb81bcb32018-09-19 03:50:03 +0000212 AST = buildASTFromCode("void f() { struct A { A(int); }; int x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000213 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
214 EXPECT_FALSE(isMutated(Results, AST.get()));
215
Shuai Wangb81bcb32018-09-19 03:50:03 +0000216 AST = buildASTFromCode("struct A { A(); A& operator=(A); };"
217 "void f() { A x, y; y = x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000218 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
219 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000220
221 AST = buildASTFromCode(
222 "template <int> struct A { A(); A(const A&); static void mf(A) {} };"
223 "void f() { A<0> x; A<0>::mf(x); }");
224 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
225 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000226}
227
228TEST(ExprMutationAnalyzerTest, ByConstValueArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000229 auto AST = buildASTFromCode("void g(const int); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000230 auto Results =
231 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
232 EXPECT_FALSE(isMutated(Results, AST.get()));
233
Shuai Wangb81bcb32018-09-19 03:50:03 +0000234 AST = buildASTFromCode("void g(int* const); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000235 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
236 EXPECT_FALSE(isMutated(Results, AST.get()));
237
Shuai Wangb81bcb32018-09-19 03:50:03 +0000238 AST = buildASTFromCode("typedef int* const CIntPtr;"
239 "void g(CIntPtr); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000240 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
241 EXPECT_FALSE(isMutated(Results, AST.get()));
242
Shuai Wangb81bcb32018-09-19 03:50:03 +0000243 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000244 "struct A {}; A operator+(const A, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000245 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
246 EXPECT_FALSE(isMutated(Results, AST.get()));
247
Shuai Wangb81bcb32018-09-19 03:50:03 +0000248 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000249 "void f() { struct A { A(const int); }; int x; A y(x); }");
250 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
251 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000252
253 AST = buildASTFromCode("template <int> struct A { A(); A(const A&);"
254 "static void mf(const A&) {} };"
255 "void f() { A<0> x; A<0>::mf(x); }");
256 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
257 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000258}
259
260TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000261 auto AST = buildASTFromCode("void g(int&); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000262 auto Results =
263 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
264 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
265
Shuai Wangb81bcb32018-09-19 03:50:03 +0000266 AST = buildASTFromCode("typedef int& IntRef;"
267 "void g(IntRef); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000268 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
269 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
270
Shuai Wangb81bcb32018-09-19 03:50:03 +0000271 AST = buildASTFromCode("template <class T> using TRef = T&;"
272 "void g(TRef<int>); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000273 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
274 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
275
Shuai Wangb81bcb32018-09-19 03:50:03 +0000276 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000277 "template <class T> struct identity { using type = T; };"
278 "template <class T, class U = T&> void g(typename identity<U>::type);"
279 "void f() { int x; g<int>(x); }");
280 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
281 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g<int>(x)"));
282
Shuai Wangb81bcb32018-09-19 03:50:03 +0000283 AST = buildASTFromCode("typedef int* IntPtr;"
284 "void g(IntPtr&); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000285 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
286 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
287
Shuai Wangb81bcb32018-09-19 03:50:03 +0000288 AST = buildASTFromCode("typedef int* IntPtr; typedef IntPtr& IntPtrRef;"
289 "void g(IntPtrRef); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000290 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
291 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
292
Shuai Wangb81bcb32018-09-19 03:50:03 +0000293 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000294 "struct A {}; A operator+(A&, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000295 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
296 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x + 1"));
297
Shuai Wangb81bcb32018-09-19 03:50:03 +0000298 AST = buildASTFromCode("void f() { struct A { A(int&); }; int x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000299 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
300 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
301
Shuai Wangb81bcb32018-09-19 03:50:03 +0000302 AST = buildASTFromCode("void f() { struct A { A(); A(A&); }; A x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000303 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
304 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000305
306 AST = buildASTFromCode(
307 "template <int> struct A { A(); A(const A&); static void mf(A&) {} };"
308 "void f() { A<0> x; A<0>::mf(x); }");
309 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
310 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("A<0>::mf(x)"));
Shuai Wange9192f82018-09-11 21:13:20 +0000311}
312
313TEST(ExprMutationAnalyzerTest, ByConstRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000314 auto AST = buildASTFromCode("void g(const int&); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000315 auto Results =
316 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
317 EXPECT_FALSE(isMutated(Results, AST.get()));
318
Shuai Wangb81bcb32018-09-19 03:50:03 +0000319 AST = buildASTFromCode("typedef const int& CIntRef;"
320 "void g(CIntRef); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000321 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
322 EXPECT_FALSE(isMutated(Results, AST.get()));
323
Shuai Wangb81bcb32018-09-19 03:50:03 +0000324 AST = buildASTFromCode("template <class T> using CTRef = const T&;"
325 "void g(CTRef<int>); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000326 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
327 EXPECT_FALSE(isMutated(Results, AST.get()));
328
Shuai Wangb81bcb32018-09-19 03:50:03 +0000329 AST =
330 buildASTFromCode("template <class T> struct identity { using type = T; };"
331 "template <class T, class U = const T&>"
332 "void g(typename identity<U>::type);"
333 "void f() { int x; g<int>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000334 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
335 EXPECT_FALSE(isMutated(Results, AST.get()));
336
Shuai Wangb81bcb32018-09-19 03:50:03 +0000337 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000338 "struct A {}; A operator+(const A&, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000339 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
340 EXPECT_FALSE(isMutated(Results, AST.get()));
341
Shuai Wangb81bcb32018-09-19 03:50:03 +0000342 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000343 "void f() { struct A { A(const int&); }; int x; A y(x); }");
344 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
345 EXPECT_FALSE(isMutated(Results, AST.get()));
346
Shuai Wangb81bcb32018-09-19 03:50:03 +0000347 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000348 "void f() { struct A { A(); A(const A&); }; A x; A y(x); }");
349 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
350 EXPECT_FALSE(isMutated(Results, AST.get()));
351}
352
353TEST(ExprMutationAnalyzerTest, ByNonConstRRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000354 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000355 "void g(int&&); void f() { int x; g(static_cast<int &&>(x)); }");
356 auto Results =
357 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
358 EXPECT_THAT(mutatedBy(Results, AST.get()),
359 ElementsAre("g(static_cast<int &&>(x))"));
360
Shuai Wang66908022018-09-19 20:27:25 +0000361 AST = buildASTFromCode("struct A {}; A operator+(A&&, int);"
Shuai Wangb81bcb32018-09-19 03:50:03 +0000362 "void f() { A x; static_cast<A &&>(x) + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000363 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
364 EXPECT_THAT(mutatedBy(Results, AST.get()),
365 ElementsAre("static_cast<A &&>(x) + 1"));
366
Shuai Wangb81bcb32018-09-19 03:50:03 +0000367 AST = buildASTFromCode("void f() { struct A { A(int&&); }; "
368 "int x; A y(static_cast<int &&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000369 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
370 EXPECT_THAT(mutatedBy(Results, AST.get()),
371 ElementsAre("static_cast<int &&>(x)"));
372
Shuai Wangb81bcb32018-09-19 03:50:03 +0000373 AST = buildASTFromCode("void f() { struct A { A(); A(A&&); }; "
374 "A x; A y(static_cast<A &&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000375 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
376 EXPECT_THAT(mutatedBy(Results, AST.get()),
377 ElementsAre("static_cast<A &&>(x)"));
378}
379
380TEST(ExprMutationAnalyzerTest, ByConstRRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000381 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000382 "void g(const int&&); void f() { int x; g(static_cast<int&&>(x)); }");
383 auto Results =
384 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
385 EXPECT_FALSE(isMutated(Results, AST.get()));
386
Shuai Wang66908022018-09-19 20:27:25 +0000387 AST = buildASTFromCode("struct A {}; A operator+(const A&&, int);"
Shuai Wangb81bcb32018-09-19 03:50:03 +0000388 "void f() { A x; static_cast<A&&>(x) + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000389 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
390 EXPECT_FALSE(isMutated(Results, AST.get()));
391
Shuai Wangb81bcb32018-09-19 03:50:03 +0000392 AST = buildASTFromCode("void f() { struct A { A(const int&&); }; "
393 "int x; A y(static_cast<int&&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000394 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
395 EXPECT_FALSE(isMutated(Results, AST.get()));
396
Shuai Wangb81bcb32018-09-19 03:50:03 +0000397 AST = buildASTFromCode("void f() { struct A { A(); A(const A&&); }; "
398 "A x; A y(static_cast<A&&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000399 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
400 EXPECT_FALSE(isMutated(Results, AST.get()));
401}
402
403TEST(ExprMutationAnalyzerTest, Move) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000404 auto AST = buildASTFromCode(StdRemoveReference + StdMove +
405 "void f() { struct A {}; A x; std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000406 auto Results =
Shuai Wange9192f82018-09-11 21:13:20 +0000407 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wang43059932018-09-17 20:10:56 +0000408 EXPECT_FALSE(isMutated(Results, AST.get()));
409
Shuai Wangb81bcb32018-09-19 03:50:03 +0000410 AST = buildASTFromCode(StdRemoveReference + StdMove +
411 "void f() { struct A {}; A x, y; std::move(x) = y; }");
Shuai Wang43059932018-09-17 20:10:56 +0000412 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
413 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("std::move(x) = y"));
414
Shuai Wangb81bcb32018-09-19 03:50:03 +0000415 AST = buildASTFromCode(StdRemoveReference + StdMove +
416 "void f() { int x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000417 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
418 EXPECT_FALSE(isMutated(Results, AST.get()));
419
420 AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000421 buildASTFromCode(StdRemoveReference + StdMove +
422 "struct S { S(); S(const S&); S& operator=(const S&); };"
423 "void f() { S x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000424 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
425 EXPECT_FALSE(isMutated(Results, AST.get()));
426
Shuai Wangb81bcb32018-09-19 03:50:03 +0000427 AST = buildASTFromCode(StdRemoveReference + StdMove +
428 "struct S { S(); S(S&&); S& operator=(S&&); };"
429 "void f() { S x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000430 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
431 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
432
Shuai Wangb81bcb32018-09-19 03:50:03 +0000433 AST = buildASTFromCode(StdRemoveReference + StdMove +
434 "struct S { S(); S(const S&); S(S&&);"
435 "S& operator=(const S&); S& operator=(S&&); };"
436 "void f() { S x, y; y = std::move(x); }");
437 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
438 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
439
440 AST = buildASTFromCode(StdRemoveReference + StdMove +
441 "struct S { S(); S(const S&); S(S&&);"
442 "S& operator=(const S&); S& operator=(S&&); };"
443 "void f() { const S x; S y; y = std::move(x); }");
444 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
445 EXPECT_FALSE(isMutated(Results, AST.get()));
446
447 AST = buildASTFromCode(StdRemoveReference + StdMove +
448 "struct S { S(); S& operator=(S); };"
449 "void f() { S x, y; y = std::move(x); }");
450 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
451 EXPECT_FALSE(isMutated(Results, AST.get()));
452
453 AST = buildASTFromCode(StdRemoveReference + StdMove +
454 "struct S{}; void f() { S x, y; y = std::move(x); }");
455 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
456 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
457
458 AST = buildASTFromCode(
Shuai Wang43059932018-09-17 20:10:56 +0000459 StdRemoveReference + StdMove +
460 "struct S{}; void f() { const S x; S y; y = std::move(x); }");
461 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
462 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000463}
464
465TEST(ExprMutationAnalyzerTest, Forward) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000466 auto AST =
467 buildASTFromCode(StdRemoveReference + StdForward +
468 "void f() { struct A {}; A x; std::forward<A &>(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000469 auto Results =
Shuai Wange9192f82018-09-11 21:13:20 +0000470 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wang43059932018-09-17 20:10:56 +0000471 EXPECT_FALSE(isMutated(Results, AST.get()));
472
Shuai Wangb81bcb32018-09-19 03:50:03 +0000473 AST = buildASTFromCode(
Shuai Wang43059932018-09-17 20:10:56 +0000474 StdRemoveReference + StdForward +
475 "void f() { struct A {}; A x, y; std::forward<A &>(x) = y; }");
476 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wange9192f82018-09-11 21:13:20 +0000477 EXPECT_THAT(mutatedBy(Results, AST.get()),
Shuai Wang43059932018-09-17 20:10:56 +0000478 ElementsAre("std::forward<A &>(x) = y"));
Shuai Wange9192f82018-09-11 21:13:20 +0000479}
480
481TEST(ExprMutationAnalyzerTest, CallUnresolved) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000482 auto AST =
483 buildASTFromCodeWithArgs("template <class T> void f() { T x; g(x); }",
484 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000485 auto Results =
486 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
487 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
488
Shuai Wangb81bcb32018-09-19 03:50:03 +0000489 AST =
490 buildASTFromCodeWithArgs("template <int N> void f() { char x[N]; g(x); }",
491 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000492 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
493 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
494
Shuai Wangb81bcb32018-09-19 03:50:03 +0000495 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000496 "template <class T> void f(T t) { int x; g(t, x); }",
497 {"-fno-delayed-template-parsing"});
498 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
499 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(t, x)"));
500
Shuai Wangb81bcb32018-09-19 03:50:03 +0000501 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000502 "template <class T> void f(T t) { int x; t.mf(x); }",
503 {"-fno-delayed-template-parsing"});
504 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
505 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("t.mf(x)"));
506
Shuai Wangb81bcb32018-09-19 03:50:03 +0000507 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000508 "template <class T> struct S;"
509 "template <class T> void f() { S<T> s; int x; s.mf(x); }",
510 {"-fno-delayed-template-parsing"});
511 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
512 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf(x)"));
513
Shuai Wangb81bcb32018-09-19 03:50:03 +0000514 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000515 "struct S { template <class T> void mf(); };"
516 "template <class T> void f(S s) { int x; s.mf<T>(x); }",
517 {"-fno-delayed-template-parsing"});
518 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
519 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf<T>(x)"));
520
Shuai Wangb81bcb32018-09-19 03:50:03 +0000521 AST = buildASTFromCodeWithArgs("template <class F>"
522 "void g(F f) { int x; f(x); } ",
523 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000524 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
525 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("f(x)"));
526
Shuai Wangb81bcb32018-09-19 03:50:03 +0000527 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000528 "template <class T> void f() { int x; (void)T(x); }",
529 {"-fno-delayed-template-parsing"});
530 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
531 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("T(x)"));
532}
533
534TEST(ExprMutationAnalyzerTest, ReturnAsValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000535 auto AST = buildASTFromCode("int f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000536 auto Results =
537 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
538 EXPECT_FALSE(isMutated(Results, AST.get()));
539
Shuai Wangb81bcb32018-09-19 03:50:03 +0000540 AST = buildASTFromCode("int* f() { int* x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000541 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
542 EXPECT_FALSE(isMutated(Results, AST.get()));
543
Shuai Wangb81bcb32018-09-19 03:50:03 +0000544 AST = buildASTFromCode("typedef int* IntPtr;"
545 "IntPtr f() { int* x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000546 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
547 EXPECT_FALSE(isMutated(Results, AST.get()));
548}
549
550TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000551 const auto AST = buildASTFromCode("int& f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000552 const auto Results =
553 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
554 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("return x;"));
555}
556
557TEST(ExprMutationAnalyzerTest, ReturnAsConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000558 const auto AST = buildASTFromCode("const int& f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000559 const auto Results =
560 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
561 EXPECT_FALSE(isMutated(Results, AST.get()));
562}
563
564TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000565 const auto AST =
566 buildASTFromCode("int&& f() { int x; return static_cast<int &&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000567 const auto Results =
568 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
569 EXPECT_THAT(mutatedBy(Results, AST.get()),
570 ElementsAre("return static_cast<int &&>(x);"));
571}
572
573TEST(ExprMutationAnalyzerTest, ReturnAsConstRRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000574 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000575 "const int&& f() { int x; return static_cast<int&&>(x); }");
576 const auto Results =
577 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
578 EXPECT_FALSE(isMutated(Results, AST.get()));
579}
580
581TEST(ExprMutationAnalyzerTest, TakeAddress) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000582 const auto AST = buildASTFromCode("void g(int*); void f() { int x; g(&x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000583 const auto Results =
584 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
585 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("&x"));
586}
587
588TEST(ExprMutationAnalyzerTest, ArrayToPointerDecay) {
589 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000590 buildASTFromCode("void g(int*); void f() { int x[2]; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000591 const auto Results =
592 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
593 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
594}
595
596TEST(ExprMutationAnalyzerTest, TemplateWithArrayToPointerDecay) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000597 const auto AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000598 "template <typename T> struct S { static constexpr int v = 8; };"
599 "template <> struct S<int> { static constexpr int v = 4; };"
600 "void g(char*);"
601 "template <typename T> void f() { char x[S<T>::v]; g(x); }"
602 "template <> void f<int>() { char y[S<int>::v]; g(y); }",
603 {"-fno-delayed-template-parsing"});
604 const auto ResultsX =
605 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
606 EXPECT_THAT(mutatedBy(ResultsX, AST.get()), ElementsAre("g(x)"));
607 const auto ResultsY =
608 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
609 EXPECT_THAT(mutatedBy(ResultsY, AST.get()), ElementsAre("y"));
610}
611
612TEST(ExprMutationAnalyzerTest, FollowRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000613 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000614 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
615 "int& r3 = r2; r3 = 10; }");
616 auto Results =
617 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
618 EXPECT_THAT(mutatedBy(Results, AST.get()),
619 ElementsAre("r0", "r1", "r2", "r3", "r3 = 10"));
620
Shuai Wangb81bcb32018-09-19 03:50:03 +0000621 AST = buildASTFromCode("typedef int& IntRefX;"
622 "using IntRefY = int&;"
623 "void f() { int x; IntRefX r0 = x; IntRefY r1 = r0;"
624 "decltype((x)) r2 = r1; r2 = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000625 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
626 EXPECT_THAT(mutatedBy(Results, AST.get()),
627 ElementsAre("r0", "r1", "r2", "r2 = 10"));
628}
629
630TEST(ExprMutationAnalyzerTest, FollowRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000631 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000632 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
633 "int& r3 = r2; int& r4 = r3; int& r5 = r4;}");
634 auto Results =
635 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
636 EXPECT_FALSE(isMutated(Results, AST.get()));
637
Shuai Wangb81bcb32018-09-19 03:50:03 +0000638 AST = buildASTFromCode("void f() { int x; int& r0 = x; const int& r1 = r0;}");
Shuai Wange9192f82018-09-11 21:13:20 +0000639 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
640 EXPECT_FALSE(isMutated(Results, AST.get()));
641
Shuai Wangb81bcb32018-09-19 03:50:03 +0000642 AST = buildASTFromCode("typedef const int& CIntRefX;"
643 "using CIntRefY = const int&;"
644 "void f() { int x; int& r0 = x; CIntRefX r1 = r0;"
645 "CIntRefY r2 = r1; decltype((r1)) r3 = r2;}");
Shuai Wange9192f82018-09-11 21:13:20 +0000646 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
647 EXPECT_FALSE(isMutated(Results, AST.get()));
648}
649
650TEST(ExprMutationAnalyzerTest, FollowConditionalRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000651 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000652 "void f() { int x, y; bool b; int &r = b ? x : y; r = 10; }");
653 const auto Results =
654 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
655 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("r", "r = 10"));
656}
657
658TEST(ExprMutationAnalyzerTest, FollowConditionalRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000659 const auto AST =
660 buildASTFromCode("void f() { int x, y; bool b; int& r = b ? x : y; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000661 const auto Results =
662 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
663 EXPECT_FALSE(isMutated(Results, AST.get()));
664}
665
Shuai Wangcb98b702018-09-14 20:07:18 +0000666TEST(ExprMutationAnalyzerTest, FollowFuncArgModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000667 auto AST = buildASTFromCode("template <class T> void g(T&& t) { t = 10; }"
668 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000669 auto Results =
670 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
671 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
672
Shuai Wangb81bcb32018-09-19 03:50:03 +0000673 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000674 "void h(int&);"
675 "template <class... Args> void g(Args&&... args) { h(args...); }"
676 "void f() { int x; g(x); }");
677 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
678 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
679
Shuai Wangb81bcb32018-09-19 03:50:03 +0000680 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000681 "void h(int&, int);"
682 "template <class... Args> void g(Args&&... args) { h(args...); }"
683 "void f() { int x, y; g(x, y); }");
684 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
685 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x, y)"));
686 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
687 EXPECT_FALSE(isMutated(Results, AST.get()));
688
Shuai Wangb81bcb32018-09-19 03:50:03 +0000689 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000690 "void h(int, int&);"
691 "template <class... Args> void g(Args&&... args) { h(args...); }"
692 "void f() { int x, y; g(y, x); }");
693 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
694 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(y, x)"));
695 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
696 EXPECT_FALSE(isMutated(Results, AST.get()));
697
Shuai Wangb81bcb32018-09-19 03:50:03 +0000698 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t = 10; } };"
699 "void f() { int x; S s(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000700 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
701 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
702
Shuai Wangb81bcb32018-09-19 03:50:03 +0000703 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000704 "struct S { template <class T> S(T&& t) : m(++t) { } int m; };"
705 "void f() { int x; S s(x); }");
706 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
707 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
Shuai Wang43059932018-09-17 20:10:56 +0000708
Shuai Wang86e5cb02018-09-19 18:00:55 +0000709 AST = buildASTFromCode("template <class U> struct S {"
710 "template <class T> S(T&& t) : m(++t) { } U m; };"
711 "void f() { int x; S<int> s(x); }");
712 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
713 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
714
Shuai Wangb81bcb32018-09-19 03:50:03 +0000715 AST = buildASTFromCode(StdRemoveReference + StdForward +
716 "template <class... Args> void u(Args&...);"
717 "template <class... Args> void h(Args&&... args)"
718 "{ u(std::forward<Args>(args)...); }"
719 "template <class... Args> void g(Args&&... args)"
720 "{ h(std::forward<Args>(args)...); }"
721 "void f() { int x; g(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000722 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
723 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
Shuai Wangcb98b702018-09-14 20:07:18 +0000724}
725
726TEST(ExprMutationAnalyzerTest, FollowFuncArgNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000727 auto AST = buildASTFromCode("template <class T> void g(T&&) {}"
728 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000729 auto Results =
730 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
731 EXPECT_FALSE(isMutated(Results, AST.get()));
732
Shuai Wangb81bcb32018-09-19 03:50:03 +0000733 AST = buildASTFromCode("template <class T> void g(T&& t) { t; }"
734 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000735 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
736 EXPECT_FALSE(isMutated(Results, AST.get()));
737
Shuai Wangb81bcb32018-09-19 03:50:03 +0000738 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
739 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000740 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
741 EXPECT_FALSE(isMutated(Results, AST.get()));
742
Shuai Wangb81bcb32018-09-19 03:50:03 +0000743 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
744 "void f() { int y, x; g(y, x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000745 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
746 EXPECT_FALSE(isMutated(Results, AST.get()));
747
Shuai Wangb81bcb32018-09-19 03:50:03 +0000748 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000749 "void h(int, int&);"
750 "template <class... Args> void g(Args&&... args) { h(args...); }"
751 "void f() { int x, y; g(x, y); }");
752 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
753 EXPECT_FALSE(isMutated(Results, AST.get()));
754
Shuai Wangb81bcb32018-09-19 03:50:03 +0000755 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t; } };"
756 "void f() { int x; S s(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000757 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
758 EXPECT_FALSE(isMutated(Results, AST.get()));
759
Shuai Wangb81bcb32018-09-19 03:50:03 +0000760 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000761 "struct S { template <class T> S(T&& t) : m(t) { } int m; };"
762 "void f() { int x; S s(x); }");
763 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
764 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang43059932018-09-17 20:10:56 +0000765
Shuai Wang86e5cb02018-09-19 18:00:55 +0000766 AST = buildASTFromCode("template <class U> struct S {"
767 "template <class T> S(T&& t) : m(t) { } U m; };"
768 "void f() { int x; S<int> s(x); }");
769 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
770 EXPECT_FALSE(isMutated(Results, AST.get()));
771
Shuai Wangb81bcb32018-09-19 03:50:03 +0000772 AST = buildASTFromCode(StdRemoveReference + StdForward +
773 "template <class... Args> void u(Args...);"
774 "template <class... Args> void h(Args&&... args)"
775 "{ u(std::forward<Args>(args)...); }"
776 "template <class... Args> void g(Args&&... args)"
777 "{ h(std::forward<Args>(args)...); }"
778 "void f() { int x; g(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000779 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
780 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wangcb98b702018-09-14 20:07:18 +0000781}
782
Shuai Wange9192f82018-09-11 21:13:20 +0000783TEST(ExprMutationAnalyzerTest, ArrayElementModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000784 const auto AST = buildASTFromCode("void f() { int x[2]; x[0] = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000785 const auto Results =
786 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
787 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x[0] = 10"));
788}
789
790TEST(ExprMutationAnalyzerTest, ArrayElementNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000791 const auto AST = buildASTFromCode("void f() { int x[2]; x[0]; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000792 const auto Results =
793 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
794 EXPECT_FALSE(isMutated(Results, AST.get()));
795}
796
797TEST(ExprMutationAnalyzerTest, NestedMemberModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000798 auto AST =
799 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
800 "struct C { B vb; }; C x; x.vb.va.vi = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000801 auto Results =
802 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
803 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.vb.va.vi = 10"));
804
Shuai Wangb81bcb32018-09-19 03:50:03 +0000805 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000806 "template <class T> void f() { T x; x.y.z = 10; }",
807 {"-fno-delayed-template-parsing"});
808 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
809 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
810
Shuai Wangb81bcb32018-09-19 03:50:03 +0000811 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000812 "template <class T> struct S;"
813 "template <class T> void f() { S<T> x; x.y.z = 10; }",
814 {"-fno-delayed-template-parsing"});
815 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
816 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
817}
818
819TEST(ExprMutationAnalyzerTest, NestedMemberNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000820 auto AST =
821 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
822 "struct C { B vb; }; C x; x.vb.va.vi; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000823 auto Results =
824 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
825 EXPECT_FALSE(isMutated(Results, AST.get()));
826
Shuai Wangb81bcb32018-09-19 03:50:03 +0000827 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.y.z; }",
828 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000829 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
830 EXPECT_FALSE(isMutated(Results, AST.get()));
831
Shuai Wangb81bcb32018-09-19 03:50:03 +0000832 AST =
833 buildASTFromCodeWithArgs("template <class T> struct S;"
834 "template <class T> void f() { S<T> x; x.y.z; }",
835 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000836 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
837 EXPECT_FALSE(isMutated(Results, AST.get()));
838}
839
840TEST(ExprMutationAnalyzerTest, CastToValue) {
841 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000842 buildASTFromCode("void f() { int x; static_cast<double>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000843 const auto Results =
844 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
845 EXPECT_FALSE(isMutated(Results, AST.get()));
846}
847
848TEST(ExprMutationAnalyzerTest, CastToRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000849 auto AST =
850 buildASTFromCode("void f() { int x; static_cast<int &>(x) = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000851 auto Results =
852 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
853 EXPECT_THAT(mutatedBy(Results, AST.get()),
854 ElementsAre("static_cast<int &>(x) = 10"));
855
Shuai Wangb81bcb32018-09-19 03:50:03 +0000856 AST = buildASTFromCode("typedef int& IntRef;"
857 "void f() { int x; static_cast<IntRef>(x) = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000858 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
859 EXPECT_THAT(mutatedBy(Results, AST.get()),
860 ElementsAre("static_cast<IntRef>(x) = 10"));
861}
862
863TEST(ExprMutationAnalyzerTest, CastToRefNotModified) {
864 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000865 buildASTFromCode("void f() { int x; static_cast<int&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000866 const auto Results =
867 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
868 EXPECT_FALSE(isMutated(Results, AST.get()));
869}
870
871TEST(ExprMutationAnalyzerTest, CastToConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000872 auto AST =
873 buildASTFromCode("void f() { int x; static_cast<const int&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000874 auto Results =
875 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
876 EXPECT_FALSE(isMutated(Results, AST.get()));
877
Shuai Wangb81bcb32018-09-19 03:50:03 +0000878 AST = buildASTFromCode("typedef const int& CIntRef;"
879 "void f() { int x; static_cast<CIntRef>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000880 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
881 EXPECT_FALSE(isMutated(Results, AST.get()));
882}
883
Petar Jovanoviceb399912019-03-07 15:50:52 +0000884TEST(ExprMutationAnalyzerTest, CommaExprWithAnAssigment) {
885 const auto AST =
886 buildASTFromCodeWithArgs("void f() { int x; int y; (x, y) = 5; }",
887 {"-Wno-unused-value"});
888 const auto Results =
889 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
890 EXPECT_TRUE(isMutated(Results, AST.get()));
891}
892
893TEST(ExprMutationAnalyzerTest, CommaExprWithDecOp) {
894 const auto AST =
895 buildASTFromCodeWithArgs("void f() { int x; int y; (x, y)++; }",
896 {"-Wno-unused-value"});
897 const auto Results =
898 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
899 EXPECT_TRUE(isMutated(Results, AST.get()));
900}
901
902TEST(ExprMutationAnalyzerTest, CommaExprWithNonConstMemberCall) {
903 const auto AST =
904 buildASTFromCodeWithArgs("class A { public: int mem; void f() { mem ++; } };"
905 "void fn() { A o1, o2; (o1, o2).f(); }",
906 {"-Wno-unused-value"});
907 const auto Results =
908 match(withEnclosingCompound(declRefTo("o2")), AST->getASTContext());
909 EXPECT_TRUE(isMutated(Results, AST.get()));
910}
911
912TEST(ExprMutationAnalyzerTest, CommaExprWithConstMemberCall) {
913 const auto AST =
914 buildASTFromCodeWithArgs("class A { public: int mem; void f() const { } };"
915 "void fn() { A o1, o2; (o1, o2).f(); }",
916 {"-Wno-unused-value"});
917 const auto Results =
918 match(withEnclosingCompound(declRefTo("o2")), AST->getASTContext());
919 EXPECT_FALSE(isMutated(Results, AST.get()));
920}
921
922TEST(ExprMutationAnalyzerTest, CommaExprWithCallExpr) {
923 const auto AST =
924 buildASTFromCodeWithArgs("class A { public: int mem; void f(A &O1) {} };"
925 "void fn() { A o1, o2; o2.f((o2, o1)); }",
926 {"-Wno-unused-value"});
927 const auto Results =
928 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
929 EXPECT_TRUE(isMutated(Results, AST.get()));
930}
931
932TEST(ExprMutationAnalyzerTest, CommaExprWithCallUnresolved) {
933 auto AST = buildASTFromCodeWithArgs(
934 "template <class T> struct S;"
935 "template <class T> void f() { S<T> s; int x, y; s.mf((y, x)); }",
Reid Kleckner15846bb2019-03-07 18:57:04 +0000936 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
Petar Jovanoviceb399912019-03-07 15:50:52 +0000937 auto Results =
938 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
939 EXPECT_TRUE(isMutated(Results, AST.get()));
940
941 AST = buildASTFromCodeWithArgs(
942 "template <class T> void f(T t) { int x, y; g(t, (y, x)); }",
Reid Kleckner15846bb2019-03-07 18:57:04 +0000943 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
Petar Jovanoviceb399912019-03-07 15:50:52 +0000944 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
945 EXPECT_TRUE(isMutated(Results, AST.get()));
946}
947
948TEST(ExprMutationAnalyzerTest, CommaExprParmRef) {
949 const auto AST =
950 buildASTFromCodeWithArgs("class A { public: int mem;};"
951 "extern void fn(A &o1);"
952 "void fn2 () { A o1, o2; fn((o2, o1)); } ",
953 {"-Wno-unused-value"});
954 const auto Results =
955 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
956 EXPECT_TRUE(isMutated(Results, AST.get()));
957}
958
959TEST(ExprMutationAnalyzerTest, CommaExprWithAmpersandOp) {
960 const auto AST =
961 buildASTFromCodeWithArgs("class A { public: int mem;};"
962 "void fn () { A o1, o2;"
963 "void *addr = &(o2, o1); } ",
964 {"-Wno-unused-value"});
965 const auto Results =
966 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
967 EXPECT_TRUE(isMutated(Results, AST.get()));
968}
969
970TEST(ExprMutationAnalyzerTest, CommaExprAsReturnAsValue) {
971 auto AST = buildASTFromCodeWithArgs("int f() { int x, y; return (x, y); }",
972 {"-Wno-unused-value"});
973 auto Results =
974 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
975 EXPECT_FALSE(isMutated(Results, AST.get()));
976}
977
978TEST(ExprMutationAnalyzerTest, CommaEpxrAsReturnAsNonConstRef) {
979 const auto AST =
980 buildASTFromCodeWithArgs("int& f() { int x, y; return (y, x); }",
981 {"-Wno-unused-value"});
982 const auto Results =
983 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
984 EXPECT_TRUE(isMutated(Results, AST.get()));
985}
986
987TEST(ExprMutationAnalyzerTest, CommaExprAsArrayToPointerDecay) {
988 const auto AST =
989 buildASTFromCodeWithArgs("void g(int*); "
990 "void f() { int x[2], y[2]; g((y, x)); }",
991 {"-Wno-unused-value"});
992 const auto Results =
993 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
994 EXPECT_TRUE(isMutated(Results, AST.get()));
995}
996
997TEST(ExprMutationAnalyzerTest, CommaExprAsUniquePtr) {
998 const std::string UniquePtrDef =
999 "template <class T> struct UniquePtr {"
1000 " UniquePtr();"
1001 " UniquePtr(const UniquePtr&) = delete;"
1002 " T& operator*() const;"
1003 " T* operator->() const;"
1004 "};";
1005 const auto AST = buildASTFromCodeWithArgs(
1006 UniquePtrDef + "template <class T> void f() "
1007 "{ UniquePtr<T> x; UniquePtr<T> y;"
1008 " (y, x)->mf(); }",
Reid Kleckner15846bb2019-03-07 18:57:04 +00001009 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
Petar Jovanoviceb399912019-03-07 15:50:52 +00001010 const auto Results =
1011 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1012 EXPECT_TRUE(isMutated(Results, AST.get()));
1013}
1014
Shuai Wange9192f82018-09-11 21:13:20 +00001015TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001016 const auto AST = buildASTFromCode("void f() { int x; [=]() { x; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001017 const auto Results =
1018 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1019 EXPECT_FALSE(isMutated(Results, AST.get()));
1020}
1021
1022TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001023 const auto AST = buildASTFromCode("void f() { int x; [x]() { x; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001024 const auto Results =
1025 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1026 EXPECT_FALSE(isMutated(Results, AST.get()));
1027}
1028
1029TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001030 const auto AST = buildASTFromCode("void f() { int x; [&]() { x = 10; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001031 const auto Results =
1032 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1033 EXPECT_THAT(mutatedBy(Results, AST.get()),
1034 ElementsAre(ResultOf(removeSpace, "[&](){x=10;}")));
1035}
1036
1037TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001038 const auto AST = buildASTFromCode("void f() { int x; [&x]() { x = 10; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001039 const auto Results =
1040 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1041 EXPECT_THAT(mutatedBy(Results, AST.get()),
1042 ElementsAre(ResultOf(removeSpace, "[&x](){x=10;}")));
1043}
1044
1045TEST(ExprMutationAnalyzerTest, RangeForArrayByRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001046 auto AST =
1047 buildASTFromCode("void f() { int x[2]; for (int& e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001048 auto Results =
1049 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1050 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
1051
Shuai Wangb81bcb32018-09-19 03:50:03 +00001052 AST = buildASTFromCode("typedef int& IntRef;"
1053 "void f() { int x[2]; for (IntRef e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001054 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1055 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
1056}
1057
1058TEST(ExprMutationAnalyzerTest, RangeForArrayByRefNotModified) {
1059 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +00001060 buildASTFromCode("void f() { int x[2]; for (int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001061 const auto Results =
1062 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1063 EXPECT_FALSE(isMutated(Results, AST.get()));
1064}
1065
1066TEST(ExprMutationAnalyzerTest, RangeForArrayByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001067 auto AST = buildASTFromCode("void f() { int x[2]; for (int e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001068 auto Results =
1069 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1070 EXPECT_FALSE(isMutated(Results, AST.get()));
1071
Shuai Wangb81bcb32018-09-19 03:50:03 +00001072 AST =
1073 buildASTFromCode("void f() { int* x[2]; for (int* e : x) e = nullptr; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001074 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1075 EXPECT_FALSE(isMutated(Results, AST.get()));
1076
Shuai Wangb81bcb32018-09-19 03:50:03 +00001077 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +00001078 "typedef int* IntPtr;"
1079 "void f() { int* x[2]; for (IntPtr e : x) e = nullptr; }");
1080 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1081 EXPECT_FALSE(isMutated(Results, AST.get()));
1082}
1083
1084TEST(ExprMutationAnalyzerTest, RangeForArrayByConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001085 auto AST =
1086 buildASTFromCode("void f() { int x[2]; for (const int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001087 auto Results =
1088 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1089 EXPECT_FALSE(isMutated(Results, AST.get()));
1090
Shuai Wangb81bcb32018-09-19 03:50:03 +00001091 AST = buildASTFromCode("typedef const int& CIntRef;"
1092 "void f() { int x[2]; for (CIntRef e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001093 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1094 EXPECT_FALSE(isMutated(Results, AST.get()));
1095}
1096
1097TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefModified) {
1098 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +00001099 buildASTFromCode("struct V { int* begin(); int* end(); };"
1100 "void f() { V x; for (int& e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001101 const auto Results =
1102 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1103 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
1104}
1105
1106TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001107 const auto AST = buildASTFromCode("struct V { int* begin(); int* end(); };"
1108 "void f() { V x; for (int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001109 const auto Results =
1110 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1111 EXPECT_FALSE(isMutated(Results, AST.get()));
1112}
1113
1114TEST(ExprMutationAnalyzerTest, RangeForNonArrayByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001115 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +00001116 "struct V { const int* begin() const; const int* end() const; };"
1117 "void f() { V x; for (int e : x) e; }");
1118 const auto Results =
1119 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1120 EXPECT_FALSE(isMutated(Results, AST.get()));
1121}
1122
1123TEST(ExprMutationAnalyzerTest, RangeForNonArrayByConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001124 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +00001125 "struct V { const int* begin() const; const int* end() const; };"
1126 "void f() { V x; for (const int& e : x) e; }");
1127 const auto Results =
1128 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1129 EXPECT_FALSE(isMutated(Results, AST.get()));
1130}
1131
1132TEST(ExprMutationAnalyzerTest, UnevaluatedExpressions) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001133 auto AST = buildASTFromCode("void f() { int x, y; decltype(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001134 auto Results =
1135 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1136 EXPECT_FALSE(isMutated(Results, AST.get()));
1137
Shuai Wangb81bcb32018-09-19 03:50:03 +00001138 AST = buildASTFromCode("void f() { int x, y; __typeof(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001139 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1140 EXPECT_FALSE(isMutated(Results, AST.get()));
1141
Shuai Wangb81bcb32018-09-19 03:50:03 +00001142 AST = buildASTFromCode("void f() { int x, y; __typeof__(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001143 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1144 EXPECT_FALSE(isMutated(Results, AST.get()));
1145
Shuai Wangb81bcb32018-09-19 03:50:03 +00001146 AST = buildASTFromCode("void f() { int x; sizeof(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001147 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1148 EXPECT_FALSE(isMutated(Results, AST.get()));
1149
Shuai Wangb81bcb32018-09-19 03:50:03 +00001150 AST = buildASTFromCode("void f() { int x; alignof(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001151 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1152 EXPECT_FALSE(isMutated(Results, AST.get()));
1153
Shuai Wangb81bcb32018-09-19 03:50:03 +00001154 AST = buildASTFromCode("void f() { int x; noexcept(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001155 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1156 EXPECT_FALSE(isMutated(Results, AST.get()));
1157
Shuai Wangb81bcb32018-09-19 03:50:03 +00001158 AST = buildASTFromCodeWithArgs("namespace std { class type_info; }"
1159 "void f() { int x; typeid(x = 10); }",
1160 {"-frtti"});
Shuai Wange9192f82018-09-11 21:13:20 +00001161 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1162 EXPECT_FALSE(isMutated(Results, AST.get()));
1163
Shuai Wangb81bcb32018-09-19 03:50:03 +00001164 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +00001165 "void f() { int x; _Generic(x = 10, int: 0, default: 1); }");
1166 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1167 EXPECT_FALSE(isMutated(Results, AST.get()));
1168}
1169
1170TEST(ExprMutationAnalyzerTest, NotUnevaluatedExpressions) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001171 auto AST = buildASTFromCode("void f() { int x; sizeof(int[x++]); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001172 auto Results =
1173 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1174 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x++"));
1175
Shuai Wangb81bcb32018-09-19 03:50:03 +00001176 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +00001177 "namespace std { class type_info; }"
1178 "struct A { virtual ~A(); }; struct B : A {};"
1179 "struct X { A& f(); }; void f() { X x; typeid(x.f()); }",
1180 {"-frtti"});
1181 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1182 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.f()"));
1183}
1184
1185TEST(ExprMutationAnalyzerTest, UniquePtr) {
1186 const std::string UniquePtrDef =
1187 "template <class T> struct UniquePtr {"
1188 " UniquePtr();"
1189 " UniquePtr(const UniquePtr&) = delete;"
1190 " UniquePtr(UniquePtr&&);"
1191 " UniquePtr& operator=(const UniquePtr&) = delete;"
1192 " UniquePtr& operator=(UniquePtr&&);"
1193 " T& operator*() const;"
1194 " T* operator->() const;"
1195 "};";
1196
Shuai Wangb81bcb32018-09-19 03:50:03 +00001197 auto AST = buildASTFromCode(UniquePtrDef +
1198 "void f() { UniquePtr<int> x; *x = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001199 auto Results =
1200 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1201 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("* x = 10"));
1202
Shuai Wangb81bcb32018-09-19 03:50:03 +00001203 AST = buildASTFromCode(UniquePtrDef + "void f() { UniquePtr<int> x; *x; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001204 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1205 EXPECT_FALSE(isMutated(Results, AST.get()));
1206
Shuai Wangb81bcb32018-09-19 03:50:03 +00001207 AST = buildASTFromCode(UniquePtrDef +
1208 "void f() { UniquePtr<const int> x; *x; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001209 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1210 EXPECT_FALSE(isMutated(Results, AST.get()));
1211
Shuai Wangb81bcb32018-09-19 03:50:03 +00001212 AST = buildASTFromCode(UniquePtrDef + "struct S { int v; };"
1213 "void f() { UniquePtr<S> x; x->v; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001214 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1215 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1216
Shuai Wangb81bcb32018-09-19 03:50:03 +00001217 AST = buildASTFromCode(UniquePtrDef +
1218 "struct S { int v; };"
1219 "void f() { UniquePtr<const S> x; x->v; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001220 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1221 EXPECT_FALSE(isMutated(Results, AST.get()));
1222
Shuai Wangb81bcb32018-09-19 03:50:03 +00001223 AST =
1224 buildASTFromCode(UniquePtrDef + "struct S { void mf(); };"
1225 "void f() { UniquePtr<S> x; x->mf(); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001226 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1227 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1228
Shuai Wangb81bcb32018-09-19 03:50:03 +00001229 AST = buildASTFromCode(UniquePtrDef +
1230 "struct S { void mf() const; };"
1231 "void f() { UniquePtr<const S> x; x->mf(); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001232 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1233 EXPECT_FALSE(isMutated(Results, AST.get()));
1234
Shuai Wangb81bcb32018-09-19 03:50:03 +00001235 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +00001236 UniquePtrDef + "template <class T> void f() { UniquePtr<T> x; x->mf(); }",
1237 {"-fno-delayed-template-parsing"});
1238 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1239 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x->mf()"));
1240}
1241
Jonas Toth67b7e232019-01-21 13:26:18 +00001242TEST(ExprMutationAnalyzerTest, ReproduceFailureMinimal) {
1243 const std::string Reproducer =
1244 "namespace std {"
1245 "template <class T> T forward(T & A) { return static_cast<T&&>(A); }"
1246 "template <class T> struct __bind {"
1247 " T f;"
1248 " template <class V> __bind(T v, V &&) : f(forward(v)) {}"
1249 "};"
1250 "}"
1251 "void f() {"
1252 " int x = 42;"
1253 " auto Lambda = [] {};"
1254 " std::__bind<decltype(Lambda)>(Lambda, x);"
1255 "}";
1256 auto AST11 = buildASTFromCodeWithArgs(Reproducer, {"-std=c++11"});
1257 auto Results11 =
1258 match(withEnclosingCompound(declRefTo("x")), AST11->getASTContext());
1259 EXPECT_FALSE(isMutated(Results11, AST11.get()));
1260}
Shuai Wange9192f82018-09-11 21:13:20 +00001261} // namespace clang