blob: 68c921e43988bd8fc297ec22b5523972f7d254e4 [file] [log] [blame]
Shuai Wange9192f82018-09-11 21:13:20 +00001//===---------- ExprMutationAnalyzerTest.cpp ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Tooling/Tooling.h"
Alexander Kornienko973fcc22019-01-08 16:55:13 +000014#include "llvm/ADT/SmallString.h"
Shuai Wange9192f82018-09-11 21:13:20 +000015#include "gmock/gmock.h"
16#include "gtest/gtest.h"
17#include <cctype>
18
19namespace clang {
20
21using namespace clang::ast_matchers;
22using ::testing::ElementsAre;
23using ::testing::IsEmpty;
24using ::testing::ResultOf;
25using ::testing::StartsWith;
26using ::testing::Values;
27
28namespace {
29
30using ExprMatcher = internal::Matcher<Expr>;
31using StmtMatcher = internal::Matcher<Stmt>;
32
Shuai Wangb81bcb32018-09-19 03:50:03 +000033std::unique_ptr<ASTUnit>
34buildASTFromCodeWithArgs(const Twine &Code,
35 const std::vector<std::string> &Args) {
Alexander Kornienko973fcc22019-01-08 16:55:13 +000036 SmallString<1024> CodeStorage;
37 auto AST =
38 tooling::buildASTFromCodeWithArgs(Code.toStringRef(CodeStorage), Args);
Shuai Wangb81bcb32018-09-19 03:50:03 +000039 EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
40 return AST;
41}
42
43std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code) {
44 return buildASTFromCodeWithArgs(Code, {});
45}
46
Shuai Wange9192f82018-09-11 21:13:20 +000047ExprMatcher declRefTo(StringRef Name) {
48 return declRefExpr(to(namedDecl(hasName(Name))));
49}
50
51StmtMatcher withEnclosingCompound(ExprMatcher Matcher) {
52 return expr(Matcher, hasAncestor(compoundStmt().bind("stmt"))).bind("expr");
53}
54
55bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
56 const auto *const S = selectFirst<Stmt>("stmt", Results);
57 const auto *const E = selectFirst<Expr>("expr", Results);
58 return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(E);
59}
60
61SmallVector<std::string, 1>
62mutatedBy(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
63 const auto *const S = selectFirst<Stmt>("stmt", Results);
64 SmallVector<std::string, 1> Chain;
65 ExprMutationAnalyzer Analyzer(*S, AST->getASTContext());
66 for (const auto *E = selectFirst<Expr>("expr", Results); E != nullptr;) {
67 const Stmt *By = Analyzer.findMutation(E);
68 std::string buffer;
69 llvm::raw_string_ostream stream(buffer);
70 By->printPretty(stream, nullptr, AST->getASTContext().getPrintingPolicy());
71 Chain.push_back(StringRef(stream.str()).trim().str());
72 E = dyn_cast<DeclRefExpr>(By);
73 }
74 return Chain;
75}
76
77std::string removeSpace(std::string s) {
78 s.erase(std::remove_if(s.begin(), s.end(),
79 [](char c) { return std::isspace(c); }),
80 s.end());
81 return s;
82}
83
Shuai Wang43059932018-09-17 20:10:56 +000084const std::string StdRemoveReference =
85 "namespace std {"
86 "template<class T> struct remove_reference { typedef T type; };"
87 "template<class T> struct remove_reference<T&> { typedef T type; };"
88 "template<class T> struct remove_reference<T&&> { typedef T type; }; }";
89
90const std::string StdMove =
91 "namespace std {"
92 "template<class T> typename remove_reference<T>::type&& "
93 "move(T&& t) noexcept {"
94 "return static_cast<typename remove_reference<T>::type&&>(t); } }";
95
96const std::string StdForward =
97 "namespace std {"
98 "template<class T> T&& "
99 "forward(typename remove_reference<T>::type& t) noexcept { return t; }"
100 "template<class T> T&& "
Shuai Wangb81bcb32018-09-19 03:50:03 +0000101 "forward(typename remove_reference<T>::type&& t) noexcept { return t; } }";
Shuai Wang43059932018-09-17 20:10:56 +0000102
Shuai Wange9192f82018-09-11 21:13:20 +0000103} // namespace
104
105TEST(ExprMutationAnalyzerTest, Trivial) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000106 const auto AST = buildASTFromCode("void f() { int x; x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000107 const auto Results =
108 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
109 EXPECT_FALSE(isMutated(Results, AST.get()));
110}
111
112class AssignmentTest : public ::testing::TestWithParam<std::string> {};
113
114TEST_P(AssignmentTest, AssignmentModifies) {
115 const std::string ModExpr = "x " + GetParam() + " 10";
Shuai Wangb81bcb32018-09-19 03:50:03 +0000116 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000117 const auto Results =
118 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
119 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
120}
121
122INSTANTIATE_TEST_CASE_P(AllAssignmentOperators, AssignmentTest,
123 Values("=", "+=", "-=", "*=", "/=", "%=", "&=", "|=",
124 "^=", "<<=", ">>="), );
125
126class IncDecTest : public ::testing::TestWithParam<std::string> {};
127
128TEST_P(IncDecTest, IncDecModifies) {
129 const std::string ModExpr = GetParam();
Shuai Wangb81bcb32018-09-19 03:50:03 +0000130 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000131 const auto Results =
132 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
133 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
134}
135
136INSTANTIATE_TEST_CASE_P(AllIncDecOperators, IncDecTest,
137 Values("++x", "--x", "x++", "x--"), );
138
139TEST(ExprMutationAnalyzerTest, NonConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000140 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000141 "void f() { struct Foo { void mf(); }; Foo x; x.mf(); }");
142 const auto Results =
143 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
144 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
145}
146
147TEST(ExprMutationAnalyzerTest, AssumedNonConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000148 auto AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000149 "struct X { template <class T> void mf(); };"
150 "template <class T> void f() { X x; x.mf<T>(); }",
151 {"-fno-delayed-template-parsing"});
152 auto Results =
153 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
154 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf<T>()"));
155
Shuai Wangb81bcb32018-09-19 03:50:03 +0000156 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.mf(); }",
157 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000158 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
159 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
160
Shuai Wangb81bcb32018-09-19 03:50:03 +0000161 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000162 "template <class T> struct X;"
163 "template <class T> void f() { X<T> x; x.mf(); }",
164 {"-fno-delayed-template-parsing"});
165 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
166 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
167}
168
169TEST(ExprMutationAnalyzerTest, ConstMemberFunc) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000170 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000171 "void f() { struct Foo { void mf() const; }; Foo x; x.mf(); }");
172 const auto Results =
173 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
174 EXPECT_FALSE(isMutated(Results, AST.get()));
175}
176
177TEST(ExprMutationAnalyzerTest, NonConstOperator) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000178 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000179 "void f() { struct Foo { Foo& operator=(int); }; Foo x; x = 10; }");
180 const auto Results =
181 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
182 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x = 10"));
183}
184
185TEST(ExprMutationAnalyzerTest, ConstOperator) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000186 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000187 "void f() { struct Foo { int operator()() const; }; Foo x; x(); }");
188 const auto Results =
189 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
190 EXPECT_FALSE(isMutated(Results, AST.get()));
191}
192
193TEST(ExprMutationAnalyzerTest, ByValueArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000194 auto AST = buildASTFromCode("void g(int); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000195 auto Results =
196 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
197 EXPECT_FALSE(isMutated(Results, AST.get()));
198
Shuai Wangb81bcb32018-09-19 03:50:03 +0000199 AST = buildASTFromCode("void g(int*); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000200 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
201 EXPECT_FALSE(isMutated(Results, AST.get()));
202
Shuai Wangb81bcb32018-09-19 03:50:03 +0000203 AST = buildASTFromCode("typedef int* IntPtr;"
204 "void g(IntPtr); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000205 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
206 EXPECT_FALSE(isMutated(Results, AST.get()));
207
Shuai Wangb81bcb32018-09-19 03:50:03 +0000208 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000209 "struct A {}; A operator+(A, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000210 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
211 EXPECT_FALSE(isMutated(Results, AST.get()));
212
Shuai Wangb81bcb32018-09-19 03:50:03 +0000213 AST = buildASTFromCode("void f() { struct A { A(int); }; int x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000214 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
215 EXPECT_FALSE(isMutated(Results, AST.get()));
216
Shuai Wangb81bcb32018-09-19 03:50:03 +0000217 AST = buildASTFromCode("struct A { A(); A& operator=(A); };"
218 "void f() { A x, y; y = x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000219 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
220 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000221
222 AST = buildASTFromCode(
223 "template <int> struct A { A(); A(const A&); static void mf(A) {} };"
224 "void f() { A<0> x; A<0>::mf(x); }");
225 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
226 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000227}
228
229TEST(ExprMutationAnalyzerTest, ByConstValueArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000230 auto AST = buildASTFromCode("void g(const int); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000231 auto Results =
232 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
233 EXPECT_FALSE(isMutated(Results, AST.get()));
234
Shuai Wangb81bcb32018-09-19 03:50:03 +0000235 AST = buildASTFromCode("void g(int* const); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000236 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
237 EXPECT_FALSE(isMutated(Results, AST.get()));
238
Shuai Wangb81bcb32018-09-19 03:50:03 +0000239 AST = buildASTFromCode("typedef int* const CIntPtr;"
240 "void g(CIntPtr); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000241 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
242 EXPECT_FALSE(isMutated(Results, AST.get()));
243
Shuai Wangb81bcb32018-09-19 03:50:03 +0000244 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000245 "struct A {}; A operator+(const A, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000246 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
247 EXPECT_FALSE(isMutated(Results, AST.get()));
248
Shuai Wangb81bcb32018-09-19 03:50:03 +0000249 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000250 "void f() { struct A { A(const int); }; int x; A y(x); }");
251 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
252 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000253
254 AST = buildASTFromCode("template <int> struct A { A(); A(const A&);"
255 "static void mf(const A&) {} };"
256 "void f() { A<0> x; A<0>::mf(x); }");
257 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
258 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000259}
260
261TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000262 auto AST = buildASTFromCode("void g(int&); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000263 auto Results =
264 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
265 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
266
Shuai Wangb81bcb32018-09-19 03:50:03 +0000267 AST = buildASTFromCode("typedef int& IntRef;"
268 "void g(IntRef); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000269 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
270 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
271
Shuai Wangb81bcb32018-09-19 03:50:03 +0000272 AST = buildASTFromCode("template <class T> using TRef = T&;"
273 "void g(TRef<int>); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000274 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
275 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
276
Shuai Wangb81bcb32018-09-19 03:50:03 +0000277 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000278 "template <class T> struct identity { using type = T; };"
279 "template <class T, class U = T&> void g(typename identity<U>::type);"
280 "void f() { int x; g<int>(x); }");
281 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
282 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g<int>(x)"));
283
Shuai Wangb81bcb32018-09-19 03:50:03 +0000284 AST = buildASTFromCode("typedef int* IntPtr;"
285 "void g(IntPtr&); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000286 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
287 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
288
Shuai Wangb81bcb32018-09-19 03:50:03 +0000289 AST = buildASTFromCode("typedef int* IntPtr; typedef IntPtr& IntPtrRef;"
290 "void g(IntPtrRef); void f() { int* x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000291 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
292 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
293
Shuai Wangb81bcb32018-09-19 03:50:03 +0000294 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000295 "struct A {}; A operator+(A&, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000296 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
297 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x + 1"));
298
Shuai Wangb81bcb32018-09-19 03:50:03 +0000299 AST = buildASTFromCode("void f() { struct A { A(int&); }; int x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000300 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
301 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
302
Shuai Wangb81bcb32018-09-19 03:50:03 +0000303 AST = buildASTFromCode("void f() { struct A { A(); A(A&); }; A x; A y(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000304 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
305 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
Shuai Wang86e5cb02018-09-19 18:00:55 +0000306
307 AST = buildASTFromCode(
308 "template <int> struct A { A(); A(const A&); static void mf(A&) {} };"
309 "void f() { A<0> x; A<0>::mf(x); }");
310 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
311 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("A<0>::mf(x)"));
Shuai Wange9192f82018-09-11 21:13:20 +0000312}
313
314TEST(ExprMutationAnalyzerTest, ByConstRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000315 auto AST = buildASTFromCode("void g(const int&); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000316 auto Results =
317 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
318 EXPECT_FALSE(isMutated(Results, AST.get()));
319
Shuai Wangb81bcb32018-09-19 03:50:03 +0000320 AST = buildASTFromCode("typedef const int& CIntRef;"
321 "void g(CIntRef); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000322 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
323 EXPECT_FALSE(isMutated(Results, AST.get()));
324
Shuai Wangb81bcb32018-09-19 03:50:03 +0000325 AST = buildASTFromCode("template <class T> using CTRef = const T&;"
326 "void g(CTRef<int>); void f() { int x; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000327 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
328 EXPECT_FALSE(isMutated(Results, AST.get()));
329
Shuai Wangb81bcb32018-09-19 03:50:03 +0000330 AST =
331 buildASTFromCode("template <class T> struct identity { using type = T; };"
332 "template <class T, class U = const T&>"
333 "void g(typename identity<U>::type);"
334 "void f() { int x; g<int>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000335 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
336 EXPECT_FALSE(isMutated(Results, AST.get()));
337
Shuai Wangb81bcb32018-09-19 03:50:03 +0000338 AST = buildASTFromCode(
Shuai Wang66908022018-09-19 20:27:25 +0000339 "struct A {}; A operator+(const A&, int); void f() { A x; x + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000340 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
341 EXPECT_FALSE(isMutated(Results, AST.get()));
342
Shuai Wangb81bcb32018-09-19 03:50:03 +0000343 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000344 "void f() { struct A { A(const int&); }; int x; A y(x); }");
345 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
346 EXPECT_FALSE(isMutated(Results, AST.get()));
347
Shuai Wangb81bcb32018-09-19 03:50:03 +0000348 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000349 "void f() { struct A { A(); A(const A&); }; A x; A y(x); }");
350 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
351 EXPECT_FALSE(isMutated(Results, AST.get()));
352}
353
354TEST(ExprMutationAnalyzerTest, ByNonConstRRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000355 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000356 "void g(int&&); void f() { int x; g(static_cast<int &&>(x)); }");
357 auto Results =
358 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
359 EXPECT_THAT(mutatedBy(Results, AST.get()),
360 ElementsAre("g(static_cast<int &&>(x))"));
361
Shuai Wang66908022018-09-19 20:27:25 +0000362 AST = buildASTFromCode("struct A {}; A operator+(A&&, int);"
Shuai Wangb81bcb32018-09-19 03:50:03 +0000363 "void f() { A x; static_cast<A &&>(x) + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000364 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
365 EXPECT_THAT(mutatedBy(Results, AST.get()),
366 ElementsAre("static_cast<A &&>(x) + 1"));
367
Shuai Wangb81bcb32018-09-19 03:50:03 +0000368 AST = buildASTFromCode("void f() { struct A { A(int&&); }; "
369 "int x; A y(static_cast<int &&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000370 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
371 EXPECT_THAT(mutatedBy(Results, AST.get()),
372 ElementsAre("static_cast<int &&>(x)"));
373
Shuai Wangb81bcb32018-09-19 03:50:03 +0000374 AST = buildASTFromCode("void f() { struct A { A(); A(A&&); }; "
375 "A x; A y(static_cast<A &&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000376 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
377 EXPECT_THAT(mutatedBy(Results, AST.get()),
378 ElementsAre("static_cast<A &&>(x)"));
379}
380
381TEST(ExprMutationAnalyzerTest, ByConstRRefArgument) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000382 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000383 "void g(const int&&); void f() { int x; g(static_cast<int&&>(x)); }");
384 auto Results =
385 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
386 EXPECT_FALSE(isMutated(Results, AST.get()));
387
Shuai Wang66908022018-09-19 20:27:25 +0000388 AST = buildASTFromCode("struct A {}; A operator+(const A&&, int);"
Shuai Wangb81bcb32018-09-19 03:50:03 +0000389 "void f() { A x; static_cast<A&&>(x) + 1; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000390 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
391 EXPECT_FALSE(isMutated(Results, AST.get()));
392
Shuai Wangb81bcb32018-09-19 03:50:03 +0000393 AST = buildASTFromCode("void f() { struct A { A(const int&&); }; "
394 "int x; A y(static_cast<int&&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000395 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
396 EXPECT_FALSE(isMutated(Results, AST.get()));
397
Shuai Wangb81bcb32018-09-19 03:50:03 +0000398 AST = buildASTFromCode("void f() { struct A { A(); A(const A&&); }; "
399 "A x; A y(static_cast<A&&>(x)); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000400 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
401 EXPECT_FALSE(isMutated(Results, AST.get()));
402}
403
404TEST(ExprMutationAnalyzerTest, Move) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000405 auto AST = buildASTFromCode(StdRemoveReference + StdMove +
406 "void f() { struct A {}; A x; std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000407 auto Results =
Shuai Wange9192f82018-09-11 21:13:20 +0000408 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wang43059932018-09-17 20:10:56 +0000409 EXPECT_FALSE(isMutated(Results, AST.get()));
410
Shuai Wangb81bcb32018-09-19 03:50:03 +0000411 AST = buildASTFromCode(StdRemoveReference + StdMove +
412 "void f() { struct A {}; A x, y; std::move(x) = y; }");
Shuai Wang43059932018-09-17 20:10:56 +0000413 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
414 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("std::move(x) = y"));
415
Shuai Wangb81bcb32018-09-19 03:50:03 +0000416 AST = buildASTFromCode(StdRemoveReference + StdMove +
417 "void f() { int x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000418 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
419 EXPECT_FALSE(isMutated(Results, AST.get()));
420
421 AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000422 buildASTFromCode(StdRemoveReference + StdMove +
423 "struct S { S(); S(const S&); S& operator=(const S&); };"
424 "void f() { S x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000425 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
426 EXPECT_FALSE(isMutated(Results, AST.get()));
427
Shuai Wangb81bcb32018-09-19 03:50:03 +0000428 AST = buildASTFromCode(StdRemoveReference + StdMove +
429 "struct S { S(); S(S&&); S& operator=(S&&); };"
430 "void f() { S x, y; y = std::move(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000431 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
432 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
433
Shuai Wangb81bcb32018-09-19 03:50:03 +0000434 AST = buildASTFromCode(StdRemoveReference + StdMove +
435 "struct S { S(); S(const S&); S(S&&);"
436 "S& operator=(const S&); S& operator=(S&&); };"
437 "void f() { S x, y; y = std::move(x); }");
438 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
439 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
440
441 AST = buildASTFromCode(StdRemoveReference + StdMove +
442 "struct S { S(); S(const S&); S(S&&);"
443 "S& operator=(const S&); S& operator=(S&&); };"
444 "void f() { const S x; S y; y = std::move(x); }");
445 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
446 EXPECT_FALSE(isMutated(Results, AST.get()));
447
448 AST = buildASTFromCode(StdRemoveReference + StdMove +
449 "struct S { S(); S& operator=(S); };"
450 "void f() { S x, y; y = std::move(x); }");
451 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
452 EXPECT_FALSE(isMutated(Results, AST.get()));
453
454 AST = buildASTFromCode(StdRemoveReference + StdMove +
455 "struct S{}; void f() { S x, y; y = std::move(x); }");
456 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
457 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
458
459 AST = buildASTFromCode(
Shuai Wang43059932018-09-17 20:10:56 +0000460 StdRemoveReference + StdMove +
461 "struct S{}; void f() { const S x; S y; y = std::move(x); }");
462 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
463 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wange9192f82018-09-11 21:13:20 +0000464}
465
466TEST(ExprMutationAnalyzerTest, Forward) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000467 auto AST =
468 buildASTFromCode(StdRemoveReference + StdForward +
469 "void f() { struct A {}; A x; std::forward<A &>(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000470 auto Results =
Shuai Wange9192f82018-09-11 21:13:20 +0000471 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wang43059932018-09-17 20:10:56 +0000472 EXPECT_FALSE(isMutated(Results, AST.get()));
473
Shuai Wangb81bcb32018-09-19 03:50:03 +0000474 AST = buildASTFromCode(
Shuai Wang43059932018-09-17 20:10:56 +0000475 StdRemoveReference + StdForward +
476 "void f() { struct A {}; A x, y; std::forward<A &>(x) = y; }");
477 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
Shuai Wange9192f82018-09-11 21:13:20 +0000478 EXPECT_THAT(mutatedBy(Results, AST.get()),
Shuai Wang43059932018-09-17 20:10:56 +0000479 ElementsAre("std::forward<A &>(x) = y"));
Shuai Wange9192f82018-09-11 21:13:20 +0000480}
481
482TEST(ExprMutationAnalyzerTest, CallUnresolved) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000483 auto AST =
484 buildASTFromCodeWithArgs("template <class T> void f() { T x; g(x); }",
485 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000486 auto Results =
487 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
488 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
489
Shuai Wangb81bcb32018-09-19 03:50:03 +0000490 AST =
491 buildASTFromCodeWithArgs("template <int N> void f() { char x[N]; g(x); }",
492 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000493 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
494 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
495
Shuai Wangb81bcb32018-09-19 03:50:03 +0000496 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000497 "template <class T> void f(T t) { int x; g(t, x); }",
498 {"-fno-delayed-template-parsing"});
499 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
500 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(t, x)"));
501
Shuai Wangb81bcb32018-09-19 03:50:03 +0000502 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000503 "template <class T> void f(T t) { int x; t.mf(x); }",
504 {"-fno-delayed-template-parsing"});
505 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
506 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("t.mf(x)"));
507
Shuai Wangb81bcb32018-09-19 03:50:03 +0000508 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000509 "template <class T> struct S;"
510 "template <class T> void f() { S<T> s; int x; s.mf(x); }",
511 {"-fno-delayed-template-parsing"});
512 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
513 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf(x)"));
514
Shuai Wangb81bcb32018-09-19 03:50:03 +0000515 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000516 "struct S { template <class T> void mf(); };"
517 "template <class T> void f(S s) { int x; s.mf<T>(x); }",
518 {"-fno-delayed-template-parsing"});
519 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
520 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf<T>(x)"));
521
Shuai Wangb81bcb32018-09-19 03:50:03 +0000522 AST = buildASTFromCodeWithArgs("template <class F>"
523 "void g(F f) { int x; f(x); } ",
524 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000525 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
526 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("f(x)"));
527
Shuai Wangb81bcb32018-09-19 03:50:03 +0000528 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000529 "template <class T> void f() { int x; (void)T(x); }",
530 {"-fno-delayed-template-parsing"});
531 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
532 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("T(x)"));
533}
534
535TEST(ExprMutationAnalyzerTest, ReturnAsValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000536 auto AST = buildASTFromCode("int f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000537 auto Results =
538 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
539 EXPECT_FALSE(isMutated(Results, AST.get()));
540
Shuai Wangb81bcb32018-09-19 03:50:03 +0000541 AST = buildASTFromCode("int* f() { int* x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000542 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
543 EXPECT_FALSE(isMutated(Results, AST.get()));
544
Shuai Wangb81bcb32018-09-19 03:50:03 +0000545 AST = buildASTFromCode("typedef int* IntPtr;"
546 "IntPtr f() { int* x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000547 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
548 EXPECT_FALSE(isMutated(Results, AST.get()));
549}
550
551TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000552 const auto AST = buildASTFromCode("int& f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000553 const auto Results =
554 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
555 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("return x;"));
556}
557
558TEST(ExprMutationAnalyzerTest, ReturnAsConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000559 const auto AST = buildASTFromCode("const int& f() { int x; return x; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000560 const auto Results =
561 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
562 EXPECT_FALSE(isMutated(Results, AST.get()));
563}
564
565TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000566 const auto AST =
567 buildASTFromCode("int&& f() { int x; return static_cast<int &&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000568 const auto Results =
569 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
570 EXPECT_THAT(mutatedBy(Results, AST.get()),
571 ElementsAre("return static_cast<int &&>(x);"));
572}
573
574TEST(ExprMutationAnalyzerTest, ReturnAsConstRRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000575 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000576 "const int&& f() { int x; return static_cast<int&&>(x); }");
577 const auto Results =
578 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
579 EXPECT_FALSE(isMutated(Results, AST.get()));
580}
581
582TEST(ExprMutationAnalyzerTest, TakeAddress) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000583 const auto AST = buildASTFromCode("void g(int*); void f() { int x; g(&x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000584 const auto Results =
585 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
586 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("&x"));
587}
588
589TEST(ExprMutationAnalyzerTest, ArrayToPointerDecay) {
590 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000591 buildASTFromCode("void g(int*); void f() { int x[2]; g(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000592 const auto Results =
593 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
594 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
595}
596
597TEST(ExprMutationAnalyzerTest, TemplateWithArrayToPointerDecay) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000598 const auto AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000599 "template <typename T> struct S { static constexpr int v = 8; };"
600 "template <> struct S<int> { static constexpr int v = 4; };"
601 "void g(char*);"
602 "template <typename T> void f() { char x[S<T>::v]; g(x); }"
603 "template <> void f<int>() { char y[S<int>::v]; g(y); }",
604 {"-fno-delayed-template-parsing"});
605 const auto ResultsX =
606 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
607 EXPECT_THAT(mutatedBy(ResultsX, AST.get()), ElementsAre("g(x)"));
608 const auto ResultsY =
609 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
610 EXPECT_THAT(mutatedBy(ResultsY, AST.get()), ElementsAre("y"));
611}
612
613TEST(ExprMutationAnalyzerTest, FollowRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000614 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000615 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
616 "int& r3 = r2; r3 = 10; }");
617 auto Results =
618 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
619 EXPECT_THAT(mutatedBy(Results, AST.get()),
620 ElementsAre("r0", "r1", "r2", "r3", "r3 = 10"));
621
Shuai Wangb81bcb32018-09-19 03:50:03 +0000622 AST = buildASTFromCode("typedef int& IntRefX;"
623 "using IntRefY = int&;"
624 "void f() { int x; IntRefX r0 = x; IntRefY r1 = r0;"
625 "decltype((x)) r2 = r1; r2 = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000626 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
627 EXPECT_THAT(mutatedBy(Results, AST.get()),
628 ElementsAre("r0", "r1", "r2", "r2 = 10"));
629}
630
631TEST(ExprMutationAnalyzerTest, FollowRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000632 auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000633 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
634 "int& r3 = r2; int& r4 = r3; int& r5 = r4;}");
635 auto Results =
636 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
637 EXPECT_FALSE(isMutated(Results, AST.get()));
638
Shuai Wangb81bcb32018-09-19 03:50:03 +0000639 AST = buildASTFromCode("void f() { int x; int& r0 = x; const int& r1 = r0;}");
Shuai Wange9192f82018-09-11 21:13:20 +0000640 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
641 EXPECT_FALSE(isMutated(Results, AST.get()));
642
Shuai Wangb81bcb32018-09-19 03:50:03 +0000643 AST = buildASTFromCode("typedef const int& CIntRefX;"
644 "using CIntRefY = const int&;"
645 "void f() { int x; int& r0 = x; CIntRefX r1 = r0;"
646 "CIntRefY r2 = r1; decltype((r1)) r3 = r2;}");
Shuai Wange9192f82018-09-11 21:13:20 +0000647 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
648 EXPECT_FALSE(isMutated(Results, AST.get()));
649}
650
651TEST(ExprMutationAnalyzerTest, FollowConditionalRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000652 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000653 "void f() { int x, y; bool b; int &r = b ? x : y; r = 10; }");
654 const auto Results =
655 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
656 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("r", "r = 10"));
657}
658
659TEST(ExprMutationAnalyzerTest, FollowConditionalRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000660 const auto AST =
661 buildASTFromCode("void f() { int x, y; bool b; int& r = b ? x : y; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000662 const auto Results =
663 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
664 EXPECT_FALSE(isMutated(Results, AST.get()));
665}
666
Shuai Wangcb98b702018-09-14 20:07:18 +0000667TEST(ExprMutationAnalyzerTest, FollowFuncArgModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000668 auto AST = buildASTFromCode("template <class T> void g(T&& t) { t = 10; }"
669 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000670 auto Results =
671 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
672 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
673
Shuai Wangb81bcb32018-09-19 03:50:03 +0000674 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000675 "void h(int&);"
676 "template <class... Args> void g(Args&&... args) { h(args...); }"
677 "void f() { int x; g(x); }");
678 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
679 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
680
Shuai Wangb81bcb32018-09-19 03:50:03 +0000681 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000682 "void h(int&, int);"
683 "template <class... Args> void g(Args&&... args) { h(args...); }"
684 "void f() { int x, y; g(x, y); }");
685 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
686 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x, y)"));
687 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
688 EXPECT_FALSE(isMutated(Results, AST.get()));
689
Shuai Wangb81bcb32018-09-19 03:50:03 +0000690 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000691 "void h(int, int&);"
692 "template <class... Args> void g(Args&&... args) { h(args...); }"
693 "void f() { int x, y; g(y, x); }");
694 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
695 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(y, x)"));
696 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
697 EXPECT_FALSE(isMutated(Results, AST.get()));
698
Shuai Wangb81bcb32018-09-19 03:50:03 +0000699 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t = 10; } };"
700 "void f() { int x; S s(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000701 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
702 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
703
Shuai Wangb81bcb32018-09-19 03:50:03 +0000704 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000705 "struct S { template <class T> S(T&& t) : m(++t) { } int m; };"
706 "void f() { int x; S s(x); }");
707 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
708 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
Shuai Wang43059932018-09-17 20:10:56 +0000709
Shuai Wang86e5cb02018-09-19 18:00:55 +0000710 AST = buildASTFromCode("template <class U> struct S {"
711 "template <class T> S(T&& t) : m(++t) { } U m; };"
712 "void f() { int x; S<int> s(x); }");
713 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
714 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
715
Shuai Wangb81bcb32018-09-19 03:50:03 +0000716 AST = buildASTFromCode(StdRemoveReference + StdForward +
717 "template <class... Args> void u(Args&...);"
718 "template <class... Args> void h(Args&&... args)"
719 "{ u(std::forward<Args>(args)...); }"
720 "template <class... Args> void g(Args&&... args)"
721 "{ h(std::forward<Args>(args)...); }"
722 "void f() { int x; g(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000723 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
724 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
Shuai Wangcb98b702018-09-14 20:07:18 +0000725}
726
727TEST(ExprMutationAnalyzerTest, FollowFuncArgNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000728 auto AST = buildASTFromCode("template <class T> void g(T&&) {}"
729 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000730 auto Results =
731 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
732 EXPECT_FALSE(isMutated(Results, AST.get()));
733
Shuai Wangb81bcb32018-09-19 03:50:03 +0000734 AST = buildASTFromCode("template <class T> void g(T&& t) { t; }"
735 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000736 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
737 EXPECT_FALSE(isMutated(Results, AST.get()));
738
Shuai Wangb81bcb32018-09-19 03:50:03 +0000739 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
740 "void f() { int x; g(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000741 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
742 EXPECT_FALSE(isMutated(Results, AST.get()));
743
Shuai Wangb81bcb32018-09-19 03:50:03 +0000744 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
745 "void f() { int y, x; g(y, x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000746 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
747 EXPECT_FALSE(isMutated(Results, AST.get()));
748
Shuai Wangb81bcb32018-09-19 03:50:03 +0000749 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000750 "void h(int, int&);"
751 "template <class... Args> void g(Args&&... args) { h(args...); }"
752 "void f() { int x, y; g(x, y); }");
753 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
754 EXPECT_FALSE(isMutated(Results, AST.get()));
755
Shuai Wangb81bcb32018-09-19 03:50:03 +0000756 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t; } };"
757 "void f() { int x; S s(x); }");
Shuai Wangcb98b702018-09-14 20:07:18 +0000758 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
759 EXPECT_FALSE(isMutated(Results, AST.get()));
760
Shuai Wangb81bcb32018-09-19 03:50:03 +0000761 AST = buildASTFromCode(
Shuai Wangcb98b702018-09-14 20:07:18 +0000762 "struct S { template <class T> S(T&& t) : m(t) { } int m; };"
763 "void f() { int x; S s(x); }");
764 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
765 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wang43059932018-09-17 20:10:56 +0000766
Shuai Wang86e5cb02018-09-19 18:00:55 +0000767 AST = buildASTFromCode("template <class U> struct S {"
768 "template <class T> S(T&& t) : m(t) { } U m; };"
769 "void f() { int x; S<int> s(x); }");
770 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
771 EXPECT_FALSE(isMutated(Results, AST.get()));
772
Shuai Wangb81bcb32018-09-19 03:50:03 +0000773 AST = buildASTFromCode(StdRemoveReference + StdForward +
774 "template <class... Args> void u(Args...);"
775 "template <class... Args> void h(Args&&... args)"
776 "{ u(std::forward<Args>(args)...); }"
777 "template <class... Args> void g(Args&&... args)"
778 "{ h(std::forward<Args>(args)...); }"
779 "void f() { int x; g(x); }");
Shuai Wang43059932018-09-17 20:10:56 +0000780 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
781 EXPECT_FALSE(isMutated(Results, AST.get()));
Shuai Wangcb98b702018-09-14 20:07:18 +0000782}
783
Shuai Wange9192f82018-09-11 21:13:20 +0000784TEST(ExprMutationAnalyzerTest, ArrayElementModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000785 const auto AST = buildASTFromCode("void f() { int x[2]; x[0] = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000786 const auto Results =
787 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
788 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x[0] = 10"));
789}
790
791TEST(ExprMutationAnalyzerTest, ArrayElementNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000792 const auto AST = buildASTFromCode("void f() { int x[2]; x[0]; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000793 const auto Results =
794 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
795 EXPECT_FALSE(isMutated(Results, AST.get()));
796}
797
798TEST(ExprMutationAnalyzerTest, NestedMemberModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000799 auto AST =
800 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
801 "struct C { B vb; }; C x; x.vb.va.vi = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000802 auto Results =
803 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
804 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.vb.va.vi = 10"));
805
Shuai Wangb81bcb32018-09-19 03:50:03 +0000806 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000807 "template <class T> void f() { T x; x.y.z = 10; }",
808 {"-fno-delayed-template-parsing"});
809 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
810 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
811
Shuai Wangb81bcb32018-09-19 03:50:03 +0000812 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +0000813 "template <class T> struct S;"
814 "template <class T> void f() { S<T> x; x.y.z = 10; }",
815 {"-fno-delayed-template-parsing"});
816 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
817 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
818}
819
820TEST(ExprMutationAnalyzerTest, NestedMemberNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000821 auto AST =
822 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
823 "struct C { B vb; }; C x; x.vb.va.vi; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000824 auto Results =
825 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
826 EXPECT_FALSE(isMutated(Results, AST.get()));
827
Shuai Wangb81bcb32018-09-19 03:50:03 +0000828 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.y.z; }",
829 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000830 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
831 EXPECT_FALSE(isMutated(Results, AST.get()));
832
Shuai Wangb81bcb32018-09-19 03:50:03 +0000833 AST =
834 buildASTFromCodeWithArgs("template <class T> struct S;"
835 "template <class T> void f() { S<T> x; x.y.z; }",
836 {"-fno-delayed-template-parsing"});
Shuai Wange9192f82018-09-11 21:13:20 +0000837 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
838 EXPECT_FALSE(isMutated(Results, AST.get()));
839}
840
841TEST(ExprMutationAnalyzerTest, CastToValue) {
842 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000843 buildASTFromCode("void f() { int x; static_cast<double>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000844 const auto Results =
845 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
846 EXPECT_FALSE(isMutated(Results, AST.get()));
847}
848
849TEST(ExprMutationAnalyzerTest, CastToRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000850 auto AST =
851 buildASTFromCode("void f() { int x; static_cast<int &>(x) = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000852 auto Results =
853 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
854 EXPECT_THAT(mutatedBy(Results, AST.get()),
855 ElementsAre("static_cast<int &>(x) = 10"));
856
Shuai Wangb81bcb32018-09-19 03:50:03 +0000857 AST = buildASTFromCode("typedef int& IntRef;"
858 "void f() { int x; static_cast<IntRef>(x) = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000859 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
860 EXPECT_THAT(mutatedBy(Results, AST.get()),
861 ElementsAre("static_cast<IntRef>(x) = 10"));
862}
863
864TEST(ExprMutationAnalyzerTest, CastToRefNotModified) {
865 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000866 buildASTFromCode("void f() { int x; static_cast<int&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000867 const auto Results =
868 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
869 EXPECT_FALSE(isMutated(Results, AST.get()));
870}
871
872TEST(ExprMutationAnalyzerTest, CastToConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000873 auto AST =
874 buildASTFromCode("void f() { int x; static_cast<const int&>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000875 auto Results =
876 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
877 EXPECT_FALSE(isMutated(Results, AST.get()));
878
Shuai Wangb81bcb32018-09-19 03:50:03 +0000879 AST = buildASTFromCode("typedef const int& CIntRef;"
880 "void f() { int x; static_cast<CIntRef>(x); }");
Shuai Wange9192f82018-09-11 21:13:20 +0000881 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
882 EXPECT_FALSE(isMutated(Results, AST.get()));
883}
884
885TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000886 const auto AST = buildASTFromCode("void f() { int x; [=]() { x; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000887 const auto Results =
888 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
889 EXPECT_FALSE(isMutated(Results, AST.get()));
890}
891
892TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000893 const auto AST = buildASTFromCode("void f() { int x; [x]() { x; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000894 const auto Results =
895 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
896 EXPECT_FALSE(isMutated(Results, AST.get()));
897}
898
899TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000900 const auto AST = buildASTFromCode("void f() { int x; [&]() { x = 10; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000901 const auto Results =
902 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
903 EXPECT_THAT(mutatedBy(Results, AST.get()),
904 ElementsAre(ResultOf(removeSpace, "[&](){x=10;}")));
905}
906
907TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000908 const auto AST = buildASTFromCode("void f() { int x; [&x]() { x = 10; }; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000909 const auto Results =
910 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
911 EXPECT_THAT(mutatedBy(Results, AST.get()),
912 ElementsAre(ResultOf(removeSpace, "[&x](){x=10;}")));
913}
914
915TEST(ExprMutationAnalyzerTest, RangeForArrayByRefModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000916 auto AST =
917 buildASTFromCode("void f() { int x[2]; for (int& e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000918 auto Results =
919 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
920 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
921
Shuai Wangb81bcb32018-09-19 03:50:03 +0000922 AST = buildASTFromCode("typedef int& IntRef;"
923 "void f() { int x[2]; for (IntRef e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000924 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
925 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
926}
927
928TEST(ExprMutationAnalyzerTest, RangeForArrayByRefNotModified) {
929 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000930 buildASTFromCode("void f() { int x[2]; for (int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000931 const auto Results =
932 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
933 EXPECT_FALSE(isMutated(Results, AST.get()));
934}
935
936TEST(ExprMutationAnalyzerTest, RangeForArrayByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000937 auto AST = buildASTFromCode("void f() { int x[2]; for (int e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000938 auto Results =
939 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
940 EXPECT_FALSE(isMutated(Results, AST.get()));
941
Shuai Wangb81bcb32018-09-19 03:50:03 +0000942 AST =
943 buildASTFromCode("void f() { int* x[2]; for (int* e : x) e = nullptr; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000944 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
945 EXPECT_FALSE(isMutated(Results, AST.get()));
946
Shuai Wangb81bcb32018-09-19 03:50:03 +0000947 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000948 "typedef int* IntPtr;"
949 "void f() { int* x[2]; for (IntPtr e : x) e = nullptr; }");
950 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
951 EXPECT_FALSE(isMutated(Results, AST.get()));
952}
953
954TEST(ExprMutationAnalyzerTest, RangeForArrayByConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000955 auto AST =
956 buildASTFromCode("void f() { int x[2]; for (const int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000957 auto Results =
958 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
959 EXPECT_FALSE(isMutated(Results, AST.get()));
960
Shuai Wangb81bcb32018-09-19 03:50:03 +0000961 AST = buildASTFromCode("typedef const int& CIntRef;"
962 "void f() { int x[2]; for (CIntRef e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000963 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
964 EXPECT_FALSE(isMutated(Results, AST.get()));
965}
966
967TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefModified) {
968 const auto AST =
Shuai Wangb81bcb32018-09-19 03:50:03 +0000969 buildASTFromCode("struct V { int* begin(); int* end(); };"
970 "void f() { V x; for (int& e : x) e = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000971 const auto Results =
972 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
973 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10"));
974}
975
976TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefNotModified) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000977 const auto AST = buildASTFromCode("struct V { int* begin(); int* end(); };"
978 "void f() { V x; for (int& e : x) e; }");
Shuai Wange9192f82018-09-11 21:13:20 +0000979 const auto Results =
980 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
981 EXPECT_FALSE(isMutated(Results, AST.get()));
982}
983
984TEST(ExprMutationAnalyzerTest, RangeForNonArrayByValue) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000985 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000986 "struct V { const int* begin() const; const int* end() const; };"
987 "void f() { V x; for (int e : x) e; }");
988 const auto Results =
989 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
990 EXPECT_FALSE(isMutated(Results, AST.get()));
991}
992
993TEST(ExprMutationAnalyzerTest, RangeForNonArrayByConstRef) {
Shuai Wangb81bcb32018-09-19 03:50:03 +0000994 const auto AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +0000995 "struct V { const int* begin() const; const int* end() const; };"
996 "void f() { V x; for (const int& e : x) e; }");
997 const auto Results =
998 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
999 EXPECT_FALSE(isMutated(Results, AST.get()));
1000}
1001
1002TEST(ExprMutationAnalyzerTest, UnevaluatedExpressions) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001003 auto AST = buildASTFromCode("void f() { int x, y; decltype(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001004 auto Results =
1005 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1006 EXPECT_FALSE(isMutated(Results, AST.get()));
1007
Shuai Wangb81bcb32018-09-19 03:50:03 +00001008 AST = buildASTFromCode("void f() { int x, y; __typeof(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001009 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1010 EXPECT_FALSE(isMutated(Results, AST.get()));
1011
Shuai Wangb81bcb32018-09-19 03:50:03 +00001012 AST = buildASTFromCode("void f() { int x, y; __typeof__(x = 10) z = y; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001013 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1014 EXPECT_FALSE(isMutated(Results, AST.get()));
1015
Shuai Wangb81bcb32018-09-19 03:50:03 +00001016 AST = buildASTFromCode("void f() { int x; sizeof(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001017 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1018 EXPECT_FALSE(isMutated(Results, AST.get()));
1019
Shuai Wangb81bcb32018-09-19 03:50:03 +00001020 AST = buildASTFromCode("void f() { int x; alignof(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001021 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1022 EXPECT_FALSE(isMutated(Results, AST.get()));
1023
Shuai Wangb81bcb32018-09-19 03:50:03 +00001024 AST = buildASTFromCode("void f() { int x; noexcept(x = 10); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001025 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1026 EXPECT_FALSE(isMutated(Results, AST.get()));
1027
Shuai Wangb81bcb32018-09-19 03:50:03 +00001028 AST = buildASTFromCodeWithArgs("namespace std { class type_info; }"
1029 "void f() { int x; typeid(x = 10); }",
1030 {"-frtti"});
Shuai Wange9192f82018-09-11 21:13:20 +00001031 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1032 EXPECT_FALSE(isMutated(Results, AST.get()));
1033
Shuai Wangb81bcb32018-09-19 03:50:03 +00001034 AST = buildASTFromCode(
Shuai Wange9192f82018-09-11 21:13:20 +00001035 "void f() { int x; _Generic(x = 10, int: 0, default: 1); }");
1036 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1037 EXPECT_FALSE(isMutated(Results, AST.get()));
1038}
1039
1040TEST(ExprMutationAnalyzerTest, NotUnevaluatedExpressions) {
Shuai Wangb81bcb32018-09-19 03:50:03 +00001041 auto AST = buildASTFromCode("void f() { int x; sizeof(int[x++]); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001042 auto Results =
1043 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1044 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x++"));
1045
Shuai Wangb81bcb32018-09-19 03:50:03 +00001046 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +00001047 "namespace std { class type_info; }"
1048 "struct A { virtual ~A(); }; struct B : A {};"
1049 "struct X { A& f(); }; void f() { X x; typeid(x.f()); }",
1050 {"-frtti"});
1051 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1052 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.f()"));
1053}
1054
1055TEST(ExprMutationAnalyzerTest, UniquePtr) {
1056 const std::string UniquePtrDef =
1057 "template <class T> struct UniquePtr {"
1058 " UniquePtr();"
1059 " UniquePtr(const UniquePtr&) = delete;"
1060 " UniquePtr(UniquePtr&&);"
1061 " UniquePtr& operator=(const UniquePtr&) = delete;"
1062 " UniquePtr& operator=(UniquePtr&&);"
1063 " T& operator*() const;"
1064 " T* operator->() const;"
1065 "};";
1066
Shuai Wangb81bcb32018-09-19 03:50:03 +00001067 auto AST = buildASTFromCode(UniquePtrDef +
1068 "void f() { UniquePtr<int> x; *x = 10; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001069 auto Results =
1070 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1071 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("* x = 10"));
1072
Shuai Wangb81bcb32018-09-19 03:50:03 +00001073 AST = buildASTFromCode(UniquePtrDef + "void f() { UniquePtr<int> x; *x; }");
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(UniquePtrDef +
1078 "void f() { UniquePtr<const int> x; *x; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001079 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1080 EXPECT_FALSE(isMutated(Results, AST.get()));
1081
Shuai Wangb81bcb32018-09-19 03:50:03 +00001082 AST = buildASTFromCode(UniquePtrDef + "struct S { int v; };"
1083 "void f() { UniquePtr<S> x; x->v; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001084 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1085 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1086
Shuai Wangb81bcb32018-09-19 03:50:03 +00001087 AST = buildASTFromCode(UniquePtrDef +
1088 "struct S { int v; };"
1089 "void f() { UniquePtr<const S> x; x->v; }");
Shuai Wange9192f82018-09-11 21:13:20 +00001090 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1091 EXPECT_FALSE(isMutated(Results, AST.get()));
1092
Shuai Wangb81bcb32018-09-19 03:50:03 +00001093 AST =
1094 buildASTFromCode(UniquePtrDef + "struct S { void mf(); };"
1095 "void f() { UniquePtr<S> x; x->mf(); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001096 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1097 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1098
Shuai Wangb81bcb32018-09-19 03:50:03 +00001099 AST = buildASTFromCode(UniquePtrDef +
1100 "struct S { void mf() const; };"
1101 "void f() { UniquePtr<const S> x; x->mf(); }");
Shuai Wange9192f82018-09-11 21:13:20 +00001102 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1103 EXPECT_FALSE(isMutated(Results, AST.get()));
1104
Shuai Wangb81bcb32018-09-19 03:50:03 +00001105 AST = buildASTFromCodeWithArgs(
Shuai Wange9192f82018-09-11 21:13:20 +00001106 UniquePtrDef + "template <class T> void f() { UniquePtr<T> x; x->mf(); }",
1107 {"-fno-delayed-template-parsing"});
1108 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1109 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x->mf()"));
1110}
1111
1112} // namespace clang