Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Analysis / ExprMutationAnalyzerTest.cpp
blobc0a398394093c485864ea85d71256f674a400df2
1 //===---------- ExprMutationAnalyzerTest.cpp ------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
10 #include "clang/AST/TypeLoc.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Tooling/Tooling.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 #include <cctype>
19 namespace clang {
21 using namespace clang::ast_matchers;
22 using ::testing::ElementsAre;
23 using ::testing::ResultOf;
24 using ::testing::Values;
26 namespace {
28 using ExprMatcher = internal::Matcher<Expr>;
29 using StmtMatcher = internal::Matcher<Stmt>;
31 std::unique_ptr<ASTUnit>
32 buildASTFromCodeWithArgs(const Twine &Code,
33 const std::vector<std::string> &Args) {
34 SmallString<1024> CodeStorage;
35 auto AST =
36 tooling::buildASTFromCodeWithArgs(Code.toStringRef(CodeStorage), Args);
37 EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
38 return AST;
41 std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code) {
42 return buildASTFromCodeWithArgs(Code, {});
45 ExprMatcher declRefTo(StringRef Name) {
46 return declRefExpr(to(namedDecl(hasName(Name))));
49 StmtMatcher withEnclosingCompound(ExprMatcher Matcher) {
50 return expr(Matcher, hasAncestor(compoundStmt().bind("stmt"))).bind("expr");
53 bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
54 const auto *const S = selectFirst<Stmt>("stmt", Results);
55 const auto *const E = selectFirst<Expr>("expr", Results);
56 TraversalKindScope RAII(AST->getASTContext(), TK_AsIs);
57 return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(E);
60 SmallVector<std::string, 1>
61 mutatedBy(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());
66 for (const auto *E = selectFirst<Expr>("expr", Results); E != nullptr;) {
67 const Stmt *By = Analyzer.findMutation(E);
68 if (!By)
69 break;
71 std::string Buffer;
72 llvm::raw_string_ostream Stream(Buffer);
73 By->printPretty(Stream, nullptr, AST->getASTContext().getPrintingPolicy());
74 Chain.emplace_back(StringRef(Stream.str()).trim().str());
75 E = dyn_cast<DeclRefExpr>(By);
77 return Chain;
80 std::string removeSpace(std::string s) {
81 s.erase(std::remove_if(s.begin(), s.end(),
82 [](char c) { return llvm::isSpace(c); }),
83 s.end());
84 return s;
87 const std::string StdRemoveReference =
88 "namespace std {"
89 "template<class T> struct remove_reference { typedef T type; };"
90 "template<class T> struct remove_reference<T&> { typedef T type; };"
91 "template<class T> struct remove_reference<T&&> { typedef T type; }; }";
93 const std::string StdMove =
94 "namespace std {"
95 "template<class T> typename remove_reference<T>::type&& "
96 "move(T&& t) noexcept {"
97 "return static_cast<typename remove_reference<T>::type&&>(t); } }";
99 const std::string StdForward =
100 "namespace std {"
101 "template<class T> T&& "
102 "forward(typename remove_reference<T>::type& t) noexcept { return t; }"
103 "template<class T> T&& "
104 "forward(typename remove_reference<T>::type&& t) noexcept { return t; } }";
106 } // namespace
108 TEST(ExprMutationAnalyzerTest, Trivial) {
109 const auto AST = buildASTFromCode("void f() { int x; x; }");
110 const auto Results =
111 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
112 EXPECT_FALSE(isMutated(Results, AST.get()));
115 class AssignmentTest : public ::testing::TestWithParam<std::string> {};
117 // This test is for the most basic and direct modification of a variable,
118 // assignment to it (e.g. `x = 10;`).
119 // It additionally tests that references to a variable are not only captured
120 // directly but expressions that result in the variable are handled, too.
121 // This includes the comma operator, parens and the ternary operator.
122 TEST_P(AssignmentTest, AssignmentModifies) {
123 // Test the detection of the raw expression modifications.
125 const std::string ModExpr = "x " + GetParam() + " 10";
126 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
127 const auto Results =
128 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
129 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
132 // Test the detection if the expression is surrounded by parens.
134 const std::string ModExpr = "(x) " + GetParam() + " 10";
135 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
136 const auto Results =
137 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
138 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
141 // Test the detection if the comma operator yields the expression as result.
143 const std::string ModExpr = "x " + GetParam() + " 10";
144 const auto AST = buildASTFromCodeWithArgs(
145 "void f() { int x, y; y, " + ModExpr + "; }", {"-Wno-unused-value"});
146 const auto Results =
147 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
148 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
151 // Ensure no detection if the comma operator does not yield the expression as
152 // result.
154 const std::string ModExpr = "y, x, y " + GetParam() + " 10";
155 const auto AST = buildASTFromCodeWithArgs(
156 "void f() { int x, y; " + ModExpr + "; }", {"-Wno-unused-value"});
157 const auto Results =
158 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
159 EXPECT_FALSE(isMutated(Results, AST.get()));
162 // Test the detection if the a ternary operator can result in the expression.
164 const std::string ModExpr = "(y != 0 ? y : x) " + GetParam() + " 10";
165 const auto AST =
166 buildASTFromCode("void f() { int y = 0, x; " + ModExpr + "; }");
167 const auto Results =
168 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
169 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
172 // Test the detection if the a ternary operator can result in the expression
173 // through multiple nesting of ternary operators.
175 const std::string ModExpr =
176 "(y != 0 ? (y > 5 ? y : x) : (y)) " + GetParam() + " 10";
177 const auto AST =
178 buildASTFromCode("void f() { int y = 0, x; " + ModExpr + "; }");
179 const auto Results =
180 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
181 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
184 // Test the detection if the a ternary operator can result in the expression
185 // with additional parens.
187 const std::string ModExpr = "(y != 0 ? (y) : ((x))) " + GetParam() + " 10";
188 const auto AST =
189 buildASTFromCode("void f() { int y = 0, x; " + ModExpr + "; }");
190 const auto Results =
191 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
192 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
195 // Test the detection for the binary conditional operator.
197 const std::string ModExpr = "(y ?: x) " + GetParam() + " 10";
198 const auto AST =
199 buildASTFromCode("void f() { int y = 0, x; " + ModExpr + "; }");
200 const auto Results =
201 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
202 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
206 INSTANTIATE_TEST_SUITE_P(AllAssignmentOperators, AssignmentTest,
207 Values("=", "+=", "-=", "*=", "/=", "%=", "&=", "|=",
208 "^=", "<<=", ">>=") );
210 TEST(ExprMutationAnalyzerTest, AssignmentConditionalWithInheritance) {
211 const auto AST = buildASTFromCode("struct Base {void nonconst(); };"
212 "struct Derived : Base {};"
213 "static void f() {"
214 " Derived x, y;"
215 " Base &b = true ? x : y;"
216 " b.nonconst();"
217 "}");
218 const auto Results =
219 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
220 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("b", "b.nonconst()"));
223 class IncDecTest : public ::testing::TestWithParam<std::string> {};
225 TEST_P(IncDecTest, IncDecModifies) {
226 const std::string ModExpr = GetParam();
227 const auto AST = buildASTFromCode("void f() { int x; " + ModExpr + "; }");
228 const auto Results =
229 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
230 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr));
233 INSTANTIATE_TEST_SUITE_P(AllIncDecOperators, IncDecTest,
234 Values("++x", "--x", "x++", "x--", "++(x)", "--(x)",
235 "(x)++", "(x)--") );
237 // Section: member functions
239 TEST(ExprMutationAnalyzerTest, NonConstMemberFunc) {
240 const auto AST = buildASTFromCode(
241 "void f() { struct Foo { void mf(); }; Foo x; x.mf(); }");
242 const auto Results =
243 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
244 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
247 TEST(ExprMutationAnalyzerTest, AssumedNonConstMemberFunc) {
248 auto AST = buildASTFromCodeWithArgs(
249 "struct X { template <class T> void mf(); };"
250 "template <class T> void f() { X x; x.mf<T>(); }",
251 {"-fno-delayed-template-parsing"});
252 auto Results =
253 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
254 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf<T>()"));
256 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.mf(); }",
257 {"-fno-delayed-template-parsing"});
258 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
259 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
261 AST = buildASTFromCodeWithArgs(
262 "template <class T> struct X;"
263 "template <class T> void f() { X<T> x; x.mf(); }",
264 {"-fno-delayed-template-parsing"});
265 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
266 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()"));
269 TEST(ExprMutationAnalyzerTest, ConstMemberFunc) {
270 const auto AST = buildASTFromCode(
271 "void f() { struct Foo { void mf() const; }; Foo x; x.mf(); }");
272 const auto Results =
273 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
274 EXPECT_FALSE(isMutated(Results, AST.get()));
277 TEST(ExprMutationAnalyzerTest, TypeDependentMemberCall) {
278 const auto AST = buildASTFromCodeWithArgs(
279 "template <class T> class vector { void push_back(T); }; "
280 "template <class T> void f() { vector<T> x; x.push_back(T()); }",
281 {"-fno-delayed-template-parsing"});
282 const auto Results =
283 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
284 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.push_back(T())"));
287 TEST(ExprMutationAnalyzerTest, MemberPointerMemberCall) {
289 const auto AST =
290 buildASTFromCode("struct X {};"
291 "using T = int (X::*)();"
292 "void f(X &x, T m) { X &ref = x; (ref.*m)(); }");
293 const auto Results =
294 match(withEnclosingCompound(declRefTo("ref")), AST->getASTContext());
295 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("(ref .* m)()"));
298 const auto AST =
299 buildASTFromCode("struct X {};"
300 "using T = int (X::*)();"
301 "void f(X &x, T const m) { X &ref = x; (ref.*m)(); }");
302 const auto Results =
303 match(withEnclosingCompound(declRefTo("ref")), AST->getASTContext());
304 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("(ref .* m)()"));
307 const auto AST =
308 buildASTFromCode("struct X {};"
309 "using T = int (X::*)() const;"
310 "void f(X &x, T m) { X &ref = x; (ref.*m)(); }");
311 const auto Results =
312 match(withEnclosingCompound(declRefTo("ref")), AST->getASTContext());
313 EXPECT_FALSE(isMutated(Results, AST.get()));
317 // Section: overloaded operators
319 TEST(ExprMutationAnalyzerTest, NonConstOperator) {
320 const auto AST = buildASTFromCode(
321 "void f() { struct Foo { Foo& operator=(int); }; Foo x; x = 10; }");
322 const auto Results =
323 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
324 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x = 10"));
327 TEST(ExprMutationAnalyzerTest, ConstOperator) {
328 const auto AST = buildASTFromCode(
329 "void f() { struct Foo { int operator()() const; }; Foo x; x(); }");
330 const auto Results =
331 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
332 EXPECT_FALSE(isMutated(Results, AST.get()));
335 TEST(ExprMutationAnalyzerTest, UnresolvedOperator) {
336 const auto AST = buildASTFromCodeWithArgs(
337 "template <typename Stream> void input_operator_template() {"
338 "Stream x; unsigned y = 42;"
339 "x >> y; }",
340 {"-fno-delayed-template-parsing"});
341 const auto Results =
342 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
343 EXPECT_TRUE(isMutated(Results, AST.get()));
346 // Section: expression as call argument
348 TEST(ExprMutationAnalyzerTest, ByValueArgument) {
349 auto AST = buildASTFromCode("void g(int); void f() { int x; g(x); }");
350 auto Results =
351 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
352 EXPECT_FALSE(isMutated(Results, AST.get()));
354 AST = buildASTFromCode("void g(int*); void f() { int* x; g(x); }");
355 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
356 EXPECT_FALSE(isMutated(Results, AST.get()));
358 AST = buildASTFromCode("typedef int* IntPtr;"
359 "void g(IntPtr); void f() { int* x; g(x); }");
360 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
361 EXPECT_FALSE(isMutated(Results, AST.get()));
363 AST = buildASTFromCode(
364 "struct A {}; A operator+(A, int); void f() { A x; x + 1; }");
365 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
366 EXPECT_FALSE(isMutated(Results, AST.get()));
368 AST = buildASTFromCode("void f() { struct A { A(int); }; int x; A y(x); }");
369 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
370 EXPECT_FALSE(isMutated(Results, AST.get()));
372 AST = buildASTFromCode("struct A { A(); A& operator=(A); };"
373 "void f() { A x, y; y = x; }");
374 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
375 EXPECT_FALSE(isMutated(Results, AST.get()));
377 AST = buildASTFromCode(
378 "template <int> struct A { A(); A(const A&); static void mf(A) {} };"
379 "void f() { A<0> x; A<0>::mf(x); }");
380 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
381 EXPECT_FALSE(isMutated(Results, AST.get()));
384 TEST(ExprMutationAnalyzerTest, ByConstValueArgument) {
385 auto AST = buildASTFromCode("void g(const int); void f() { int x; g(x); }");
386 auto Results =
387 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
388 EXPECT_FALSE(isMutated(Results, AST.get()));
390 AST = buildASTFromCode("void g(int* const); void f() { int* x; g(x); }");
391 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
392 EXPECT_FALSE(isMutated(Results, AST.get()));
394 AST = buildASTFromCode("typedef int* const CIntPtr;"
395 "void g(CIntPtr); void f() { int* x; g(x); }");
396 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
397 EXPECT_FALSE(isMutated(Results, AST.get()));
399 AST = buildASTFromCode(
400 "struct A {}; A operator+(const A, int); void f() { A x; x + 1; }");
401 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
402 EXPECT_FALSE(isMutated(Results, AST.get()));
404 AST = buildASTFromCode(
405 "void f() { struct A { A(const int); }; int x; A y(x); }");
406 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
407 EXPECT_FALSE(isMutated(Results, AST.get()));
409 AST = buildASTFromCode("template <int> struct A { A(); A(const A&);"
410 "static void mf(const A&) {} };"
411 "void f() { A<0> x; A<0>::mf(x); }");
412 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
413 EXPECT_FALSE(isMutated(Results, AST.get()));
416 TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) {
417 auto AST = buildASTFromCode("void g(int&); void f() { int x; g(x); }");
418 auto Results =
419 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
420 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
422 AST = buildASTFromCode("typedef int& IntRef;"
423 "void g(IntRef); void f() { int x; g(x); }");
424 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
425 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
427 AST = buildASTFromCode("template <class T> using TRef = T&;"
428 "void g(TRef<int>); void f() { int x; g(x); }");
429 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
430 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
432 AST = buildASTFromCode(
433 "template <class T> struct identity { using type = T; };"
434 "template <class T, class U = T&> void g(typename identity<U>::type);"
435 "void f() { int x; g<int>(x); }");
436 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
437 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g<int>(x)"));
439 AST = buildASTFromCode("typedef int* IntPtr;"
440 "void g(IntPtr&); void f() { int* x; g(x); }");
441 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
442 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
444 AST = buildASTFromCode("typedef int* IntPtr; typedef IntPtr& IntPtrRef;"
445 "void g(IntPtrRef); void f() { int* x; g(x); }");
446 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
447 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
449 AST = buildASTFromCode(
450 "struct A {}; A operator+(A&, int); void f() { A x; x + 1; }");
451 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
452 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x + 1"));
454 AST = buildASTFromCode("void f() { struct A { A(int&); }; int x; A y(x); }");
455 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
456 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
458 AST = buildASTFromCode("void f() { struct A { A(); A(A&); }; A x; A y(x); }");
459 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
460 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
462 AST = buildASTFromCode(
463 "template <int> struct A { A(); A(const A&); static void mf(A&) {} };"
464 "void f() { A<0> x; A<0>::mf(x); }");
465 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
466 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("A<0>::mf(x)"));
469 TEST(ExprMutationAnalyzerTest, ByNonConstRefArgumentFunctionTypeDependent) {
470 auto AST = buildASTFromCodeWithArgs(
471 "enum MyEnum { foo, bar };"
472 "void tryParser(unsigned& first, MyEnum Type) { first++, (void)Type; }"
473 "template <MyEnum Type> void parse() {"
474 " auto parser = [](unsigned& first) { first++; tryParser(first, Type); "
475 "};"
476 " unsigned x = 42;"
477 " parser(x);"
478 "}",
479 {"-fno-delayed-template-parsing"});
480 auto Results =
481 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
482 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("parser(x)"));
485 TEST(ExprMutationAnalyzerTest, ByConstRefArgument) {
486 auto AST = buildASTFromCode("void g(const int&); void f() { int x; g(x); }");
487 auto Results =
488 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
489 EXPECT_FALSE(isMutated(Results, AST.get()));
491 AST = buildASTFromCode("typedef const int& CIntRef;"
492 "void g(CIntRef); void f() { int x; g(x); }");
493 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
494 EXPECT_FALSE(isMutated(Results, AST.get()));
496 AST = buildASTFromCode("template <class T> using CTRef = const T&;"
497 "void g(CTRef<int>); void f() { int x; g(x); }");
498 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
499 EXPECT_FALSE(isMutated(Results, AST.get()));
501 AST =
502 buildASTFromCode("template <class T> struct identity { using type = T; };"
503 "template <class T, class U = const T&>"
504 "void g(typename identity<U>::type);"
505 "void f() { int x; g<int>(x); }");
506 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
507 EXPECT_FALSE(isMutated(Results, AST.get()));
509 AST = buildASTFromCode(
510 "struct A {}; A operator+(const A&, int); void f() { A x; x + 1; }");
511 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
512 EXPECT_FALSE(isMutated(Results, AST.get()));
514 AST = buildASTFromCode(
515 "void f() { struct A { A(const int&); }; int x; A y(x); }");
516 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
517 EXPECT_FALSE(isMutated(Results, AST.get()));
519 AST = buildASTFromCode(
520 "void f() { struct A { A(); A(const A&); }; A x; A y(x); }");
521 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
522 EXPECT_FALSE(isMutated(Results, AST.get()));
525 TEST(ExprMutationAnalyzerTest, ByNonConstRRefArgument) {
526 auto AST = buildASTFromCode(
527 "void g(int&&); void f() { int x; g(static_cast<int &&>(x)); }");
528 auto Results =
529 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
530 EXPECT_THAT(mutatedBy(Results, AST.get()),
531 ElementsAre("g(static_cast<int &&>(x))"));
533 AST = buildASTFromCode("struct A {}; A operator+(A&&, int);"
534 "void f() { A x; static_cast<A &&>(x) + 1; }");
535 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
536 EXPECT_THAT(mutatedBy(Results, AST.get()),
537 ElementsAre("static_cast<A &&>(x) + 1"));
539 AST = buildASTFromCode("void f() { struct A { A(int&&); }; "
540 "int x; A y(static_cast<int &&>(x)); }");
541 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
542 EXPECT_THAT(mutatedBy(Results, AST.get()),
543 ElementsAre("static_cast<int &&>(x)"));
545 AST = buildASTFromCode("void f() { struct A { A(); A(A&&); }; "
546 "A x; A y(static_cast<A &&>(x)); }");
547 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
548 EXPECT_THAT(mutatedBy(Results, AST.get()),
549 ElementsAre("static_cast<A &&>(x)"));
552 TEST(ExprMutationAnalyzerTest, ByConstRRefArgument) {
553 auto AST = buildASTFromCode(
554 "void g(const int&&); void f() { int x; g(static_cast<int&&>(x)); }");
555 auto Results =
556 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
557 EXPECT_THAT(mutatedBy(Results, AST.get()),
558 ElementsAre("static_cast<int &&>(x)"));
560 AST = buildASTFromCode("struct A {}; A operator+(const A&&, int);"
561 "void f() { A x; static_cast<A&&>(x) + 1; }");
562 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
563 EXPECT_THAT(mutatedBy(Results, AST.get()),
564 ElementsAre("static_cast<A &&>(x)"));
566 AST = buildASTFromCode("void f() { struct A { A(const int&&); }; "
567 "int x; A y(static_cast<int&&>(x)); }");
568 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
569 EXPECT_THAT(mutatedBy(Results, AST.get()),
570 ElementsAre("static_cast<int &&>(x)"));
572 AST = buildASTFromCode("void f() { struct A { A(); A(const A&&); }; "
573 "A x; A y(static_cast<A&&>(x)); }");
574 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
575 EXPECT_THAT(mutatedBy(Results, AST.get()),
576 ElementsAre("static_cast<A &&>(x)"));
579 // section: explicit std::move and std::forward testing
581 TEST(ExprMutationAnalyzerTest, Move) {
582 auto AST = buildASTFromCode(StdRemoveReference + StdMove +
583 "void f() { struct A {}; A x; std::move(x); }");
584 auto Results =
585 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
586 EXPECT_FALSE(isMutated(Results, AST.get()));
588 AST = buildASTFromCode(StdRemoveReference + StdMove +
589 "void f() { struct A {}; A x, y; std::move(x) = y; }");
590 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
591 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("std::move(x) = y"));
593 AST = buildASTFromCode(StdRemoveReference + StdMove +
594 "void f() { int x, y; y = std::move(x); }");
595 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
596 EXPECT_FALSE(isMutated(Results, AST.get()));
598 AST =
599 buildASTFromCode(StdRemoveReference + StdMove +
600 "struct S { S(); S(const S&); S& operator=(const S&); };"
601 "void f() { S x, y; y = std::move(x); }");
602 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
603 EXPECT_FALSE(isMutated(Results, AST.get()));
605 AST = buildASTFromCode(StdRemoveReference + StdMove +
606 "struct S { S(); S(S&&); S& operator=(S&&); };"
607 "void f() { S x, y; y = std::move(x); }");
608 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
609 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
611 AST = buildASTFromCode(StdRemoveReference + StdMove +
612 "struct S { S(); S(const S&); S(S&&);"
613 "S& operator=(const S&); S& operator=(S&&); };"
614 "void f() { S x, y; y = std::move(x); }");
615 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
616 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
618 AST = buildASTFromCode(StdRemoveReference + StdMove +
619 "struct S { S(); S(const S&); S(S&&);"
620 "S& operator=(const S&); S& operator=(S&&); };"
621 "void f() { const S x; S y; y = std::move(x); }");
622 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
623 EXPECT_FALSE(isMutated(Results, AST.get()));
625 AST = buildASTFromCode(StdRemoveReference + StdMove +
626 "struct S { S(); S& operator=(S); };"
627 "void f() { S x, y; y = std::move(x); }");
628 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
629 EXPECT_FALSE(isMutated(Results, AST.get()));
631 AST = buildASTFromCode(StdRemoveReference + StdMove +
632 "struct S{}; void f() { S x, y; y = std::move(x); }");
633 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
634 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)"));
636 AST = buildASTFromCode(
637 StdRemoveReference + StdMove +
638 "struct S{}; void f() { const S x; S y; y = std::move(x); }");
639 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
640 EXPECT_FALSE(isMutated(Results, AST.get()));
643 TEST(ExprMutationAnalyzerTest, Forward) {
644 auto AST =
645 buildASTFromCode(StdRemoveReference + StdForward +
646 "void f() { struct A {}; A x; std::forward<A &>(x); }");
647 auto Results =
648 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
649 EXPECT_FALSE(isMutated(Results, AST.get()));
651 AST = buildASTFromCode(
652 StdRemoveReference + StdForward +
653 "void f() { struct A {}; A x, y; std::forward<A &>(x) = y; }");
654 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
655 EXPECT_THAT(mutatedBy(Results, AST.get()),
656 ElementsAre("std::forward<A &>(x) = y"));
659 // section: template constellations that prohibit reasoning about modifications
660 // as it depends on instantiations.
662 TEST(ExprMutationAnalyzerTest, CallUnresolved) {
663 auto AST =
664 buildASTFromCodeWithArgs("template <class T> void f() { T x; g(x); }",
665 {"-fno-delayed-template-parsing"});
666 auto Results =
667 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
668 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
670 AST =
671 buildASTFromCodeWithArgs("template <int N> void f() { char x[N]; g(x); }",
672 {"-fno-delayed-template-parsing"});
673 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
674 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
676 AST = buildASTFromCodeWithArgs(
677 "template <class T> void f(T t) { int x; g(t, x); }",
678 {"-fno-delayed-template-parsing"});
679 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
680 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(t, x)"));
682 AST = buildASTFromCodeWithArgs(
683 "template <class T> void f(T t) { int x; t.mf(x); }",
684 {"-fno-delayed-template-parsing"});
685 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
686 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("t.mf(x)"));
688 AST = buildASTFromCodeWithArgs(
689 "template <class T> struct S;"
690 "template <class T> void f() { S<T> s; int x; s.mf(x); }",
691 {"-fno-delayed-template-parsing"});
692 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
693 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf(x)"));
695 AST = buildASTFromCodeWithArgs(
696 "struct S { template <class T> void mf(); };"
697 "template <class T> void f(S s) { int x; s.mf<T>(x); }",
698 {"-fno-delayed-template-parsing"});
699 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
700 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf<T>(x)"));
702 AST = buildASTFromCodeWithArgs("template <class F>"
703 "void g(F f) { int x; f(x); } ",
704 {"-fno-delayed-template-parsing"});
705 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
706 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("f(x)"));
708 AST = buildASTFromCodeWithArgs(
709 "template <class T> void f() { int x; (void)T(x); }",
710 {"-fno-delayed-template-parsing"});
711 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
712 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("T(x)"));
715 // section: return values
717 TEST(ExprMutationAnalyzerTest, ReturnAsValue) {
718 auto AST = buildASTFromCode("int f() { int x; return x; }");
719 auto Results =
720 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
721 EXPECT_FALSE(isMutated(Results, AST.get()));
723 AST = buildASTFromCode("int* f() { int* x; return x; }");
724 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
725 EXPECT_FALSE(isMutated(Results, AST.get()));
727 AST = buildASTFromCode("typedef int* IntPtr;"
728 "IntPtr f() { int* x; return x; }");
729 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
730 EXPECT_FALSE(isMutated(Results, AST.get()));
733 TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRef) {
734 const auto AST = buildASTFromCode("int& f() { int x; return x; }");
735 const auto Results =
736 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
737 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("return x;"));
740 TEST(ExprMutationAnalyzerTest, ReturnAsConstRef) {
741 const auto AST = buildASTFromCode("const int& f() { int x; return x; }");
742 const auto Results =
743 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
744 EXPECT_FALSE(isMutated(Results, AST.get()));
747 TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRRef) {
748 const auto AST =
749 buildASTFromCode("int&& f() { int x; return static_cast<int &&>(x); }");
750 const auto Results =
751 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
752 EXPECT_THAT(mutatedBy(Results, AST.get()),
753 ElementsAre("static_cast<int &&>(x)"));
756 TEST(ExprMutationAnalyzerTest, ReturnAsConstRRef) {
757 const auto AST = buildASTFromCode(
758 "const int&& f() { int x; return static_cast<int&&>(x); }");
759 const auto Results =
760 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
761 EXPECT_THAT(mutatedBy(Results, AST.get()),
762 ElementsAre("static_cast<int &&>(x)"));
765 // section: taking the address of a variable and pointers
767 TEST(ExprMutationAnalyzerTest, TakeAddress) {
768 const auto AST = buildASTFromCode("void g(int*); void f() { int x; g(&x); }");
769 const auto Results =
770 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
771 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("&x"));
774 TEST(ExprMutationAnalyzerTest, ArrayToPointerDecay) {
775 const auto AST =
776 buildASTFromCode("void g(int*); void f() { int x[2]; g(x); }");
777 const auto Results =
778 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
779 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
782 TEST(ExprMutationAnalyzerTest, TemplateWithArrayToPointerDecay) {
783 const auto AST = buildASTFromCodeWithArgs(
784 "template <typename T> struct S { static constexpr int v = 8; };"
785 "template <> struct S<int> { static constexpr int v = 4; };"
786 "void g(char*);"
787 "template <typename T> void f() { char x[S<T>::v]; g(x); }"
788 "template <> void f<int>() { char y[S<int>::v]; g(y); }",
789 {"-fno-delayed-template-parsing"});
790 const auto ResultsX =
791 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
792 EXPECT_THAT(mutatedBy(ResultsX, AST.get()), ElementsAre("g(x)"));
793 const auto ResultsY =
794 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
795 EXPECT_THAT(mutatedBy(ResultsY, AST.get()), ElementsAre("y"));
798 // section: special case: all created references are non-mutating themself
799 // and therefore all become 'const'/the value is not modified!
801 TEST(ExprMutationAnalyzerTest, FollowRefModified) {
802 auto AST = buildASTFromCode(
803 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
804 "int& r3 = r2; r3 = 10; }");
805 auto Results =
806 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
807 EXPECT_THAT(mutatedBy(Results, AST.get()),
808 ElementsAre("r0", "r1", "r2", "r3", "r3 = 10"));
810 AST = buildASTFromCode("typedef int& IntRefX;"
811 "using IntRefY = int&;"
812 "void f() { int x; IntRefX r0 = x; IntRefY r1 = r0;"
813 "decltype((x)) r2 = r1; r2 = 10; }");
814 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
815 EXPECT_THAT(mutatedBy(Results, AST.get()),
816 ElementsAre("r0", "r1", "r2", "r2 = 10"));
819 TEST(ExprMutationAnalyzerTest, FollowRefNotModified) {
820 auto AST = buildASTFromCode(
821 "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; "
822 "int& r3 = r2; int& r4 = r3; int& r5 = r4;}");
823 auto Results =
824 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
825 EXPECT_FALSE(isMutated(Results, AST.get()));
827 AST = buildASTFromCode("void f() { int x; int& r0 = x; const int& r1 = r0;}");
828 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
829 EXPECT_FALSE(isMutated(Results, AST.get()));
831 AST = buildASTFromCode("typedef const int& CIntRefX;"
832 "using CIntRefY = const int&;"
833 "void f() { int x; int& r0 = x; CIntRefX r1 = r0;"
834 "CIntRefY r2 = r1; decltype((r1)) r3 = r2;}");
835 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
836 EXPECT_FALSE(isMutated(Results, AST.get()));
839 TEST(ExprMutationAnalyzerTest, FollowConditionalRefModified) {
840 const auto AST = buildASTFromCode(
841 "void f() { int x, y; bool b; int &r = b ? x : y; r = 10; }");
842 const auto Results =
843 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
844 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("r", "r = 10"));
847 TEST(ExprMutationAnalyzerTest, FollowConditionalRefNotModified) {
848 const auto AST =
849 buildASTFromCode("void f() { int x, y; bool b; int& r = b ? x : y; }");
850 const auto Results =
851 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
852 EXPECT_FALSE(isMutated(Results, AST.get()));
855 TEST(ExprMutationAnalyzerTest, FollowFuncArgModified) {
856 auto AST = buildASTFromCode("template <class T> void g(T&& t) { t = 10; }"
857 "void f() { int x; g(x); }");
858 auto Results =
859 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
860 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
862 AST = buildASTFromCode(
863 "void h(int&);"
864 "template <class... Args> void g(Args&&... args) { h(args...); }"
865 "void f() { int x; g(x); }");
866 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
867 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
869 AST = buildASTFromCode(
870 "void h(int&, int);"
871 "template <class... Args> void g(Args&&... args) { h(args...); }"
872 "void f() { int x, y; g(x, y); }");
873 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
874 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x, y)"));
875 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
876 EXPECT_FALSE(isMutated(Results, AST.get()));
878 AST = buildASTFromCode(
879 "void h(int, int&);"
880 "template <class... Args> void g(Args&&... args) { h(args...); }"
881 "void f() { int x, y; g(y, x); }");
882 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
883 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(y, x)"));
884 Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
885 EXPECT_FALSE(isMutated(Results, AST.get()));
887 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t = 10; } };"
888 "void f() { int x; S s(x); }");
889 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
890 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
892 AST = buildASTFromCode(
893 "struct S { template <class T> S(T&& t) : m(++t) { } int m; };"
894 "void f() { int x; S s(x); }");
895 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
896 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
898 AST = buildASTFromCode("template <class U> struct S {"
899 "template <class T> S(T&& t) : m(++t) { } U m; };"
900 "void f() { int x; S<int> s(x); }");
901 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
902 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
904 AST = buildASTFromCode(StdRemoveReference + StdForward +
905 "template <class... Args> void u(Args&...);"
906 "template <class... Args> void h(Args&&... args)"
907 "{ u(std::forward<Args>(args)...); }"
908 "template <class... Args> void g(Args&&... args)"
909 "{ h(std::forward<Args>(args)...); }"
910 "void f() { int x; g(x); }");
911 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
912 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)"));
915 TEST(ExprMutationAnalyzerTest, FollowFuncArgNotModified) {
916 auto AST = buildASTFromCode("template <class T> void g(T&&) {}"
917 "void f() { int x; g(x); }");
918 auto Results =
919 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
920 EXPECT_FALSE(isMutated(Results, AST.get()));
922 AST = buildASTFromCode("template <class T> void g(T&& t) { t; }"
923 "void f() { int x; g(x); }");
924 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
925 EXPECT_FALSE(isMutated(Results, AST.get()));
927 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
928 "void f() { int x; g(x); }");
929 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
930 EXPECT_FALSE(isMutated(Results, AST.get()));
932 AST = buildASTFromCode("template <class... Args> void g(Args&&...) {}"
933 "void f() { int y, x; g(y, x); }");
934 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
935 EXPECT_FALSE(isMutated(Results, AST.get()));
937 AST = buildASTFromCode(
938 "void h(int, int&);"
939 "template <class... Args> void g(Args&&... args) { h(args...); }"
940 "void f() { int x, y; g(x, y); }");
941 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
942 EXPECT_FALSE(isMutated(Results, AST.get()));
944 AST = buildASTFromCode("struct S { template <class T> S(T&& t) { t; } };"
945 "void f() { int x; S s(x); }");
946 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
947 EXPECT_FALSE(isMutated(Results, AST.get()));
949 AST = buildASTFromCode(
950 "struct S { template <class T> S(T&& t) : m(t) { } int m; };"
951 "void f() { int x; S s(x); }");
952 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
953 EXPECT_FALSE(isMutated(Results, AST.get()));
955 AST = buildASTFromCode("template <class U> struct S {"
956 "template <class T> S(T&& t) : m(t) { } U m; };"
957 "void f() { int x; S<int> s(x); }");
958 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
959 EXPECT_FALSE(isMutated(Results, AST.get()));
961 AST = buildASTFromCode(StdRemoveReference + StdForward +
962 "template <class... Args> void u(Args...);"
963 "template <class... Args> void h(Args&&... args)"
964 "{ u(std::forward<Args>(args)...); }"
965 "template <class... Args> void g(Args&&... args)"
966 "{ h(std::forward<Args>(args)...); }"
967 "void f() { int x; g(x); }");
968 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
969 EXPECT_FALSE(isMutated(Results, AST.get()));
972 // section: builtin arrays
974 TEST(ExprMutationAnalyzerTest, ArrayElementModified) {
975 const auto AST = buildASTFromCode("void f() { int x[2]; x[0] = 10; }");
976 const auto Results =
977 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
978 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x[0] = 10"));
981 TEST(ExprMutationAnalyzerTest, ArrayElementNotModified) {
982 const auto AST = buildASTFromCode("void f() { int x[2]; x[0]; }");
983 const auto Results =
984 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
985 EXPECT_FALSE(isMutated(Results, AST.get()));
988 // section: member modifications
990 TEST(ExprMutationAnalyzerTest, NestedMemberModified) {
991 auto AST =
992 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
993 "struct C { B vb; }; C x; x.vb.va.vi = 10; }");
994 auto Results =
995 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
996 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.vb.va.vi = 10"));
998 AST = buildASTFromCodeWithArgs(
999 "template <class T> void f() { T x; x.y.z = 10; }",
1000 {"-fno-delayed-template-parsing"});
1001 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1002 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
1004 AST = buildASTFromCodeWithArgs(
1005 "template <class T> struct S;"
1006 "template <class T> void f() { S<T> x; x.y.z = 10; }",
1007 {"-fno-delayed-template-parsing"});
1008 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1009 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10"));
1012 TEST(ExprMutationAnalyzerTest, NestedMemberNotModified) {
1013 auto AST =
1014 buildASTFromCode("void f() { struct A { int vi; }; struct B { A va; }; "
1015 "struct C { B vb; }; C x; x.vb.va.vi; }");
1016 auto Results =
1017 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1018 EXPECT_FALSE(isMutated(Results, AST.get()));
1020 AST = buildASTFromCodeWithArgs("template <class T> void f() { T x; x.y.z; }",
1021 {"-fno-delayed-template-parsing"});
1022 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1023 EXPECT_FALSE(isMutated(Results, AST.get()));
1025 AST =
1026 buildASTFromCodeWithArgs("template <class T> struct S;"
1027 "template <class T> void f() { S<T> x; x.y.z; }",
1028 {"-fno-delayed-template-parsing"});
1029 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1030 EXPECT_FALSE(isMutated(Results, AST.get()));
1033 // section: casts
1035 TEST(ExprMutationAnalyzerTest, CastToValue) {
1036 const auto AST =
1037 buildASTFromCode("void f() { int x; static_cast<double>(x); }");
1038 const auto Results =
1039 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1040 EXPECT_FALSE(isMutated(Results, AST.get()));
1043 TEST(ExprMutationAnalyzerTest, CastToRefModified) {
1044 auto AST =
1045 buildASTFromCode("void f() { int x; static_cast<int &>(x) = 10; }");
1046 auto Results =
1047 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1048 EXPECT_THAT(mutatedBy(Results, AST.get()),
1049 ElementsAre("static_cast<int &>(x)"));
1051 AST = buildASTFromCode("typedef int& IntRef;"
1052 "void f() { int x; static_cast<IntRef>(x) = 10; }");
1053 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1054 EXPECT_THAT(mutatedBy(Results, AST.get()),
1055 ElementsAre("static_cast<IntRef>(x)"));
1058 TEST(ExprMutationAnalyzerTest, CastToRefNotModified) {
1059 const auto AST =
1060 buildASTFromCode("void f() { int x; static_cast<int&>(x); }");
1061 const auto Results =
1062 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1063 EXPECT_THAT(mutatedBy(Results, AST.get()),
1064 ElementsAre("static_cast<int &>(x)"));
1067 TEST(ExprMutationAnalyzerTest, CastToConstRef) {
1068 auto AST =
1069 buildASTFromCode("void f() { int x; static_cast<const int&>(x); }");
1070 auto Results =
1071 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1072 EXPECT_FALSE(isMutated(Results, AST.get()));
1074 AST = buildASTFromCode("typedef const int& CIntRef;"
1075 "void f() { int x; static_cast<CIntRef>(x); }");
1076 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1077 EXPECT_FALSE(isMutated(Results, AST.get()));
1080 // section: comma expressions
1082 TEST(ExprMutationAnalyzerTest, CommaExprWithAnAssigment) {
1083 const auto AST = buildASTFromCodeWithArgs(
1084 "void f() { int x; int y; (x, y) = 5; }", {"-Wno-unused-value"});
1085 const auto Results =
1086 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
1087 EXPECT_TRUE(isMutated(Results, AST.get()));
1090 TEST(ExprMutationAnalyzerTest, CommaExprWithDecOp) {
1091 const auto AST = buildASTFromCodeWithArgs(
1092 "void f() { int x; int y; (x, y)++; }", {"-Wno-unused-value"});
1093 const auto Results =
1094 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
1095 EXPECT_TRUE(isMutated(Results, AST.get()));
1098 TEST(ExprMutationAnalyzerTest, CommaExprWithNonConstMemberCall) {
1099 const auto AST = buildASTFromCodeWithArgs(
1100 "class A { public: int mem; void f() { mem ++; } };"
1101 "void fn() { A o1, o2; (o1, o2).f(); }",
1102 {"-Wno-unused-value"});
1103 const auto Results =
1104 match(withEnclosingCompound(declRefTo("o2")), AST->getASTContext());
1105 EXPECT_TRUE(isMutated(Results, AST.get()));
1108 TEST(ExprMutationAnalyzerTest, CommaExprWithConstMemberCall) {
1109 const auto AST = buildASTFromCodeWithArgs(
1110 "class A { public: int mem; void f() const { } };"
1111 "void fn() { A o1, o2; (o1, o2).f(); }",
1112 {"-Wno-unused-value"});
1113 const auto Results =
1114 match(withEnclosingCompound(declRefTo("o2")), AST->getASTContext());
1115 EXPECT_FALSE(isMutated(Results, AST.get()));
1118 TEST(ExprMutationAnalyzerTest, CommaExprWithCallExpr) {
1119 const auto AST =
1120 buildASTFromCodeWithArgs("class A { public: int mem; void f(A &O1) {} };"
1121 "void fn() { A o1, o2; o2.f((o2, o1)); }",
1122 {"-Wno-unused-value"});
1123 const auto Results =
1124 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
1125 EXPECT_TRUE(isMutated(Results, AST.get()));
1128 TEST(ExprMutationAnalyzerTest, CommaExprWithCallUnresolved) {
1129 auto AST = buildASTFromCodeWithArgs(
1130 "template <class T> struct S;"
1131 "template <class T> void f() { S<T> s; int x, y; s.mf((y, x)); }",
1132 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
1133 auto Results =
1134 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1135 EXPECT_TRUE(isMutated(Results, AST.get()));
1137 AST = buildASTFromCodeWithArgs(
1138 "template <class T> void f(T t) { int x, y; g(t, (y, x)); }",
1139 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
1140 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1141 EXPECT_TRUE(isMutated(Results, AST.get()));
1144 TEST(ExprMutationAnalyzerTest, CommaExprParmRef) {
1145 const auto AST =
1146 buildASTFromCodeWithArgs("class A { public: int mem;};"
1147 "extern void fn(A &o1);"
1148 "void fn2 () { A o1, o2; fn((o2, o1)); } ",
1149 {"-Wno-unused-value"});
1150 const auto Results =
1151 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
1152 EXPECT_TRUE(isMutated(Results, AST.get()));
1155 TEST(ExprMutationAnalyzerTest, CommaExprWithAmpersandOp) {
1156 const auto AST = buildASTFromCodeWithArgs("class A { public: int mem;};"
1157 "void fn () { A o1, o2;"
1158 "void *addr = &(o2, o1); } ",
1159 {"-Wno-unused-value"});
1160 const auto Results =
1161 match(withEnclosingCompound(declRefTo("o1")), AST->getASTContext());
1162 EXPECT_TRUE(isMutated(Results, AST.get()));
1165 TEST(ExprMutationAnalyzerTest, CommaExprAsReturnAsValue) {
1166 auto AST = buildASTFromCodeWithArgs("int f() { int x, y; return (x, y); }",
1167 {"-Wno-unused-value"});
1168 auto Results =
1169 match(withEnclosingCompound(declRefTo("y")), AST->getASTContext());
1170 EXPECT_FALSE(isMutated(Results, AST.get()));
1173 TEST(ExprMutationAnalyzerTest, CommaEpxrAsReturnAsNonConstRef) {
1174 const auto AST = buildASTFromCodeWithArgs(
1175 "int& f() { int x, y; return (y, x); }", {"-Wno-unused-value"});
1176 const auto Results =
1177 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1178 EXPECT_TRUE(isMutated(Results, AST.get()));
1181 TEST(ExprMutationAnalyzerTest, CommaExprAsArrayToPointerDecay) {
1182 const auto AST =
1183 buildASTFromCodeWithArgs("void g(int*); "
1184 "void f() { int x[2], y[2]; g((y, x)); }",
1185 {"-Wno-unused-value"});
1186 const auto Results =
1187 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1188 EXPECT_TRUE(isMutated(Results, AST.get()));
1191 TEST(ExprMutationAnalyzerTest, CommaExprAsUniquePtr) {
1192 const std::string UniquePtrDef = "template <class T> struct UniquePtr {"
1193 " UniquePtr();"
1194 " UniquePtr(const UniquePtr&) = delete;"
1195 " T& operator*() const;"
1196 " T* operator->() const;"
1197 "};";
1198 const auto AST = buildASTFromCodeWithArgs(
1199 UniquePtrDef + "template <class T> void f() "
1200 "{ UniquePtr<T> x; UniquePtr<T> y;"
1201 " (y, x)->mf(); }",
1202 {"-fno-delayed-template-parsing", "-Wno-unused-value"});
1203 const auto Results =
1204 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1205 EXPECT_TRUE(isMutated(Results, AST.get()));
1208 TEST(ExprMutationAnalyzerTest, CommaNestedConditional) {
1209 const std::string Code = "void f() { int x, y = 42;"
1210 " y, (true ? x : y) = 42; }";
1211 const auto AST = buildASTFromCodeWithArgs(Code, {"-Wno-unused-value"});
1212 const auto Results =
1213 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1214 EXPECT_THAT(mutatedBy(Results, AST.get()),
1215 ElementsAre("(true ? x : y) = 42"));
1218 // section: lambda captures
1220 TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByValue) {
1221 const auto AST = buildASTFromCode("void f() { int x; [=]() { x; }; }");
1222 const auto Results =
1223 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1224 EXPECT_FALSE(isMutated(Results, AST.get()));
1227 TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByValue) {
1228 const auto AST = buildASTFromCode("void f() { int x; [x]() { x; }; }");
1229 const auto Results =
1230 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1231 EXPECT_FALSE(isMutated(Results, AST.get()));
1234 TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByRef) {
1235 const auto AST = buildASTFromCode("void f() { int x; [&]() { x = 10; }; }");
1236 const auto Results =
1237 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1238 EXPECT_THAT(mutatedBy(Results, AST.get()),
1239 ElementsAre(ResultOf(removeSpace, "[&](){x=10;}")));
1242 TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByRef) {
1243 const auto AST = buildASTFromCode("void f() { int x; [&x]() { x = 10; }; }");
1244 const auto Results =
1245 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1246 EXPECT_THAT(mutatedBy(Results, AST.get()),
1247 ElementsAre(ResultOf(removeSpace, "[&x](){x=10;}")));
1250 // section: range-for loops
1252 TEST(ExprMutationAnalyzerTest, RangeForArrayByRefModified) {
1253 auto AST =
1254 buildASTFromCode("void f() { int x[2]; for (int& e : x) e = 10; }");
1255 auto Results =
1256 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1257 EXPECT_THAT(mutatedBy(Results, AST.get()),
1258 ElementsAre("for (int &e : x)\n e = 10;"));
1260 AST = buildASTFromCode("typedef int& IntRef;"
1261 "void f() { int x[2]; for (IntRef e : x) e = 10; }");
1262 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1263 EXPECT_THAT(mutatedBy(Results, AST.get()),
1264 ElementsAre("for (IntRef e : x)\n e = 10;"));
1267 TEST(ExprMutationAnalyzerTest, RangeForArrayByRefModifiedByImplicitInit) {
1268 const auto AST =
1269 buildASTFromCode("void f() { int x[2]; for (int& e : x) e; }");
1270 const auto Results =
1271 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1272 EXPECT_TRUE(isMutated(Results, AST.get()));
1275 TEST(ExprMutationAnalyzerTest, RangeForArrayByValue) {
1276 auto AST = buildASTFromCode("void f() { int x[2]; for (int e : x) e = 10; }");
1277 auto Results =
1278 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1279 EXPECT_FALSE(isMutated(Results, AST.get()));
1281 AST =
1282 buildASTFromCode("void f() { int* x[2]; for (int* e : x) e = nullptr; }");
1283 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1284 EXPECT_TRUE(isMutated(Results, AST.get()));
1286 AST = buildASTFromCode(
1287 "typedef int* IntPtr;"
1288 "void f() { int* x[2]; for (IntPtr e : x) e = nullptr; }");
1289 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1290 EXPECT_TRUE(isMutated(Results, AST.get()));
1293 TEST(ExprMutationAnalyzerTest, RangeForArrayByConstRef) {
1294 auto AST =
1295 buildASTFromCode("void f() { int x[2]; for (const int& e : x) e; }");
1296 auto Results =
1297 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1298 EXPECT_FALSE(isMutated(Results, AST.get()));
1300 AST = buildASTFromCode("typedef const int& CIntRef;"
1301 "void f() { int x[2]; for (CIntRef e : x) e; }");
1302 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1303 EXPECT_FALSE(isMutated(Results, AST.get()));
1306 TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefModified) {
1307 const auto AST =
1308 buildASTFromCode("struct V { int* begin(); int* end(); };"
1309 "void f() { V x; for (int& e : x) e = 10; }");
1310 const auto Results =
1311 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1312 EXPECT_THAT(mutatedBy(Results, AST.get()),
1313 ElementsAre("for (int &e : x)\n e = 10;"));
1316 TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefNotModified) {
1317 const auto AST = buildASTFromCode("struct V { int* begin(); int* end(); };"
1318 "void f() { V x; for (int& e : x) e; }");
1319 const auto Results =
1320 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1321 EXPECT_TRUE(isMutated(Results, AST.get()));
1324 TEST(ExprMutationAnalyzerTest, RangeForNonArrayByValue) {
1325 const auto AST = buildASTFromCode(
1326 "struct V { const int* begin() const; const int* end() const; };"
1327 "void f() { V x; for (int e : x) e; }");
1328 const auto Results =
1329 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1330 EXPECT_FALSE(isMutated(Results, AST.get()));
1333 TEST(ExprMutationAnalyzerTest, RangeForNonArrayByConstRef) {
1334 const auto AST = buildASTFromCode(
1335 "struct V { const int* begin() const; const int* end() const; };"
1336 "void f() { V x; for (const int& e : x) e; }");
1337 const auto Results =
1338 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1339 EXPECT_FALSE(isMutated(Results, AST.get()));
1342 // section: unevaluated expressions
1344 TEST(ExprMutationAnalyzerTest, UnevaluatedExpressions) {
1345 auto AST = buildASTFromCode("void f() { int x, y; decltype(x = 10) z = y; }");
1346 auto Results =
1347 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1348 EXPECT_FALSE(isMutated(Results, AST.get()));
1350 AST = buildASTFromCode("void f() { int x, y; __typeof(x = 10) z = y; }");
1351 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1352 EXPECT_FALSE(isMutated(Results, AST.get()));
1354 AST = buildASTFromCode("void f() { int x, y; __typeof__(x = 10) z = y; }");
1355 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1356 EXPECT_FALSE(isMutated(Results, AST.get()));
1358 AST = buildASTFromCode("void f() { int x; sizeof(x = 10); }");
1359 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1360 EXPECT_FALSE(isMutated(Results, AST.get()));
1362 AST = buildASTFromCode("void f() { int x; alignof(x = 10); }");
1363 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1364 EXPECT_FALSE(isMutated(Results, AST.get()));
1366 AST = buildASTFromCode("void f() { int x; noexcept(x = 10); }");
1367 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1368 EXPECT_FALSE(isMutated(Results, AST.get()));
1370 AST = buildASTFromCodeWithArgs("namespace std { class type_info; }"
1371 "void f() { int x; typeid(x = 10); }",
1372 {"-frtti"});
1373 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1374 EXPECT_FALSE(isMutated(Results, AST.get()));
1376 AST = buildASTFromCode(
1377 "void f() { int x; _Generic(x = 10, int: 0, default: 1); }");
1378 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1379 EXPECT_FALSE(isMutated(Results, AST.get()));
1382 TEST(ExprMutationAnalyzerTest, NotUnevaluatedExpressions) {
1383 auto AST = buildASTFromCode("void f() { int x; sizeof(int[x++]); }");
1384 auto Results =
1385 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1386 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x++"));
1388 AST = buildASTFromCodeWithArgs(
1389 "namespace std { class type_info; }"
1390 "struct A { virtual ~A(); }; struct B : A {};"
1391 "struct X { A& f(); }; void f() { X x; typeid(x.f()); }",
1392 {"-frtti"});
1393 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1394 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.f()"));
1397 // section: special case: smartpointers
1399 TEST(ExprMutationAnalyzerTest, UniquePtr) {
1400 const std::string UniquePtrDef =
1401 "template <class T> struct UniquePtr {"
1402 " UniquePtr();"
1403 " UniquePtr(const UniquePtr&) = delete;"
1404 " UniquePtr(UniquePtr&&);"
1405 " UniquePtr& operator=(const UniquePtr&) = delete;"
1406 " UniquePtr& operator=(UniquePtr&&);"
1407 " T& operator*() const;"
1408 " T* operator->() const;"
1409 "};";
1411 auto AST = buildASTFromCode(UniquePtrDef +
1412 "void f() { UniquePtr<int> x; *x = 10; }");
1413 auto Results =
1414 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1415 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("* x = 10"));
1417 AST = buildASTFromCode(UniquePtrDef + "void f() { UniquePtr<int> x; *x; }");
1418 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1419 EXPECT_FALSE(isMutated(Results, AST.get()));
1421 AST = buildASTFromCode(UniquePtrDef +
1422 "void f() { UniquePtr<const int> x; *x; }");
1423 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1424 EXPECT_FALSE(isMutated(Results, AST.get()));
1426 AST = buildASTFromCode(UniquePtrDef + "struct S { int v; };"
1427 "void f() { UniquePtr<S> x; x->v; }");
1428 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1429 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1431 AST = buildASTFromCode(UniquePtrDef +
1432 "struct S { int v; };"
1433 "void f() { UniquePtr<const S> x; x->v; }");
1434 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1435 EXPECT_FALSE(isMutated(Results, AST.get()));
1437 AST =
1438 buildASTFromCode(UniquePtrDef + "struct S { void mf(); };"
1439 "void f() { UniquePtr<S> x; x->mf(); }");
1440 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1441 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x"));
1443 AST = buildASTFromCode(UniquePtrDef +
1444 "struct S { void mf() const; };"
1445 "void f() { UniquePtr<const S> x; x->mf(); }");
1446 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1447 EXPECT_FALSE(isMutated(Results, AST.get()));
1449 AST = buildASTFromCodeWithArgs(
1450 UniquePtrDef + "template <class T> void f() { UniquePtr<T> x; x->mf(); }",
1451 {"-fno-delayed-template-parsing"});
1452 Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1453 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x->mf()"));
1456 // section: complex problems detected on real code
1458 TEST(ExprMutationAnalyzerTest, UnevaluatedContext) {
1459 const std::string Example =
1460 "template <typename T>"
1461 "struct to_construct : T { to_construct(int &j) {} };"
1462 "template <typename T>"
1463 "void placement_new_in_unique_ptr() { int x = 0;"
1464 " new to_construct<T>(x);"
1465 "}";
1466 auto AST =
1467 buildASTFromCodeWithArgs(Example, {"-fno-delayed-template-parsing"});
1468 auto Results =
1469 match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
1470 EXPECT_TRUE(isMutated(Results, AST.get()));
1471 EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("(x)"));
1474 TEST(ExprMutationAnalyzerTest, ReproduceFailureMinimal) {
1475 const std::string Reproducer =
1476 "namespace std {"
1477 "template <class T> T &forward(T &A) { return static_cast<T&&>(A); }"
1478 "template <class T> struct __bind {"
1479 " T f;"
1480 " template <class V> __bind(T v, V &&) : f(forward(v)) {}"
1481 "};"
1483 "void f() {"
1484 " int x = 42;"
1485 " auto Lambda = [] {};"
1486 " std::__bind<decltype(Lambda)>(Lambda, x);"
1487 "}";
1488 auto AST11 = buildASTFromCodeWithArgs(Reproducer, {"-std=c++11"});
1489 auto Results11 =
1490 match(withEnclosingCompound(declRefTo("x")), AST11->getASTContext());
1491 EXPECT_FALSE(isMutated(Results11, AST11.get()));
1493 } // namespace clang