Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 1 | //===---------- 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" |
| 14 | #include "gmock/gmock.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | #include <cctype> |
| 17 | |
| 18 | namespace clang { |
| 19 | |
| 20 | using namespace clang::ast_matchers; |
| 21 | using ::testing::ElementsAre; |
| 22 | using ::testing::IsEmpty; |
| 23 | using ::testing::ResultOf; |
| 24 | using ::testing::StartsWith; |
| 25 | using ::testing::Values; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | using ExprMatcher = internal::Matcher<Expr>; |
| 30 | using StmtMatcher = internal::Matcher<Stmt>; |
| 31 | |
| 32 | ExprMatcher declRefTo(StringRef Name) { |
| 33 | return declRefExpr(to(namedDecl(hasName(Name)))); |
| 34 | } |
| 35 | |
| 36 | StmtMatcher withEnclosingCompound(ExprMatcher Matcher) { |
| 37 | return expr(Matcher, hasAncestor(compoundStmt().bind("stmt"))).bind("expr"); |
| 38 | } |
| 39 | |
| 40 | bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) { |
| 41 | const auto *const S = selectFirst<Stmt>("stmt", Results); |
| 42 | const auto *const E = selectFirst<Expr>("expr", Results); |
| 43 | return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(E); |
| 44 | } |
| 45 | |
| 46 | SmallVector<std::string, 1> |
| 47 | mutatedBy(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) { |
| 48 | const auto *const S = selectFirst<Stmt>("stmt", Results); |
| 49 | SmallVector<std::string, 1> Chain; |
| 50 | ExprMutationAnalyzer Analyzer(*S, AST->getASTContext()); |
| 51 | for (const auto *E = selectFirst<Expr>("expr", Results); E != nullptr;) { |
| 52 | const Stmt *By = Analyzer.findMutation(E); |
| 53 | std::string buffer; |
| 54 | llvm::raw_string_ostream stream(buffer); |
| 55 | By->printPretty(stream, nullptr, AST->getASTContext().getPrintingPolicy()); |
| 56 | Chain.push_back(StringRef(stream.str()).trim().str()); |
| 57 | E = dyn_cast<DeclRefExpr>(By); |
| 58 | } |
| 59 | return Chain; |
| 60 | } |
| 61 | |
| 62 | std::string removeSpace(std::string s) { |
| 63 | s.erase(std::remove_if(s.begin(), s.end(), |
| 64 | [](char c) { return std::isspace(c); }), |
| 65 | s.end()); |
| 66 | return s; |
| 67 | } |
| 68 | |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 69 | const std::string StdRemoveReference = |
| 70 | "namespace std {" |
| 71 | "template<class T> struct remove_reference { typedef T type; };" |
| 72 | "template<class T> struct remove_reference<T&> { typedef T type; };" |
| 73 | "template<class T> struct remove_reference<T&&> { typedef T type; }; }"; |
| 74 | |
| 75 | const std::string StdMove = |
| 76 | "namespace std {" |
| 77 | "template<class T> typename remove_reference<T>::type&& " |
| 78 | "move(T&& t) noexcept {" |
| 79 | "return static_cast<typename remove_reference<T>::type&&>(t); } }"; |
| 80 | |
| 81 | const std::string StdForward = |
| 82 | "namespace std {" |
| 83 | "template<class T> T&& " |
| 84 | "forward(typename remove_reference<T>::type& t) noexcept { return t; }" |
| 85 | "template<class T> T&& " |
| 86 | "forward(typename remove_reference<T>::type&&) noexcept { return t; } }"; |
| 87 | |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 88 | } // namespace |
| 89 | |
| 90 | TEST(ExprMutationAnalyzerTest, Trivial) { |
| 91 | const auto AST = tooling::buildASTFromCode("void f() { int x; x; }"); |
| 92 | const auto Results = |
| 93 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 94 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 95 | } |
| 96 | |
| 97 | class AssignmentTest : public ::testing::TestWithParam<std::string> {}; |
| 98 | |
| 99 | TEST_P(AssignmentTest, AssignmentModifies) { |
| 100 | const std::string ModExpr = "x " + GetParam() + " 10"; |
| 101 | const auto AST = |
| 102 | tooling::buildASTFromCode("void f() { int x; " + ModExpr + "; }"); |
| 103 | const auto Results = |
| 104 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 105 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr)); |
| 106 | } |
| 107 | |
| 108 | INSTANTIATE_TEST_CASE_P(AllAssignmentOperators, AssignmentTest, |
| 109 | Values("=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", |
| 110 | "^=", "<<=", ">>="), ); |
| 111 | |
| 112 | class IncDecTest : public ::testing::TestWithParam<std::string> {}; |
| 113 | |
| 114 | TEST_P(IncDecTest, IncDecModifies) { |
| 115 | const std::string ModExpr = GetParam(); |
| 116 | const auto AST = |
| 117 | tooling::buildASTFromCode("void f() { int x; " + ModExpr + "; }"); |
| 118 | const auto Results = |
| 119 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 120 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre(ModExpr)); |
| 121 | } |
| 122 | |
| 123 | INSTANTIATE_TEST_CASE_P(AllIncDecOperators, IncDecTest, |
| 124 | Values("++x", "--x", "x++", "x--"), ); |
| 125 | |
| 126 | TEST(ExprMutationAnalyzerTest, NonConstMemberFunc) { |
| 127 | const auto AST = tooling::buildASTFromCode( |
| 128 | "void f() { struct Foo { void mf(); }; Foo x; x.mf(); }"); |
| 129 | const auto Results = |
| 130 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 131 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()")); |
| 132 | } |
| 133 | |
| 134 | TEST(ExprMutationAnalyzerTest, AssumedNonConstMemberFunc) { |
| 135 | auto AST = tooling::buildASTFromCodeWithArgs( |
| 136 | "struct X { template <class T> void mf(); };" |
| 137 | "template <class T> void f() { X x; x.mf<T>(); }", |
| 138 | {"-fno-delayed-template-parsing"}); |
| 139 | auto Results = |
| 140 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 141 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf<T>()")); |
| 142 | |
| 143 | AST = tooling::buildASTFromCodeWithArgs( |
| 144 | "template <class T> void f() { T x; x.mf(); }", |
| 145 | {"-fno-delayed-template-parsing"}); |
| 146 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 147 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()")); |
| 148 | |
| 149 | AST = tooling::buildASTFromCodeWithArgs( |
| 150 | "template <class T> struct X;" |
| 151 | "template <class T> void f() { X<T> x; x.mf(); }", |
| 152 | {"-fno-delayed-template-parsing"}); |
| 153 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 154 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.mf()")); |
| 155 | } |
| 156 | |
| 157 | TEST(ExprMutationAnalyzerTest, ConstMemberFunc) { |
| 158 | const auto AST = tooling::buildASTFromCode( |
| 159 | "void f() { struct Foo { void mf() const; }; Foo x; x.mf(); }"); |
| 160 | const auto Results = |
| 161 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 162 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 163 | } |
| 164 | |
| 165 | TEST(ExprMutationAnalyzerTest, NonConstOperator) { |
| 166 | const auto AST = tooling::buildASTFromCode( |
| 167 | "void f() { struct Foo { Foo& operator=(int); }; Foo x; x = 10; }"); |
| 168 | const auto Results = |
| 169 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 170 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x = 10")); |
| 171 | } |
| 172 | |
| 173 | TEST(ExprMutationAnalyzerTest, ConstOperator) { |
| 174 | const auto AST = tooling::buildASTFromCode( |
| 175 | "void f() { struct Foo { int operator()() const; }; Foo x; x(); }"); |
| 176 | const auto Results = |
| 177 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 178 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 179 | } |
| 180 | |
| 181 | TEST(ExprMutationAnalyzerTest, ByValueArgument) { |
| 182 | auto AST = |
| 183 | tooling::buildASTFromCode("void g(int); void f() { int x; g(x); }"); |
| 184 | auto Results = |
| 185 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 186 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 187 | |
| 188 | AST = tooling::buildASTFromCode("void g(int*); void f() { int* x; g(x); }"); |
| 189 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 190 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 191 | |
| 192 | AST = tooling::buildASTFromCode("typedef int* IntPtr;" |
| 193 | "void g(IntPtr); void f() { int* x; g(x); }"); |
| 194 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 195 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 196 | |
| 197 | AST = tooling::buildASTFromCode( |
| 198 | "void f() { struct A {}; A operator+(A, int); A x; x + 1; }"); |
| 199 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 200 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 201 | |
| 202 | AST = tooling::buildASTFromCode( |
| 203 | "void f() { struct A { A(int); }; int x; A y(x); }"); |
| 204 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 205 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 206 | |
| 207 | AST = tooling::buildASTFromCode( |
| 208 | "void f() { struct A { A(); A(A); }; A x; A y(x); }"); |
| 209 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 210 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 211 | } |
| 212 | |
| 213 | TEST(ExprMutationAnalyzerTest, ByConstValueArgument) { |
| 214 | auto AST = |
| 215 | tooling::buildASTFromCode("void g(const int); void f() { int x; g(x); }"); |
| 216 | auto Results = |
| 217 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 218 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 219 | |
| 220 | AST = tooling::buildASTFromCode( |
| 221 | "void g(int* const); void f() { int* x; g(x); }"); |
| 222 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 223 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 224 | |
| 225 | AST = |
| 226 | tooling::buildASTFromCode("typedef int* const CIntPtr;" |
| 227 | "void g(CIntPtr); void f() { int* x; g(x); }"); |
| 228 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 229 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 230 | |
| 231 | AST = tooling::buildASTFromCode( |
| 232 | "void f() { struct A {}; A operator+(const A, int); A x; x + 1; }"); |
| 233 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 234 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 235 | |
| 236 | AST = tooling::buildASTFromCode( |
| 237 | "void f() { struct A { A(const int); }; int x; A y(x); }"); |
| 238 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 239 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 240 | |
| 241 | AST = tooling::buildASTFromCode( |
| 242 | "void f() { struct A { A(); A(const A); }; A x; A y(x); }"); |
| 243 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 244 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 245 | } |
| 246 | |
| 247 | TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) { |
| 248 | auto AST = |
| 249 | tooling::buildASTFromCode("void g(int&); void f() { int x; g(x); }"); |
| 250 | auto Results = |
| 251 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 252 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 253 | |
| 254 | AST = tooling::buildASTFromCode("typedef int& IntRef;" |
| 255 | "void g(IntRef); void f() { int x; g(x); }"); |
| 256 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 257 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 258 | |
| 259 | AST = |
| 260 | tooling::buildASTFromCode("template <class T> using TRef = T&;" |
| 261 | "void g(TRef<int>); void f() { int x; g(x); }"); |
| 262 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 263 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 264 | |
| 265 | AST = tooling::buildASTFromCode( |
| 266 | "template <class T> struct identity { using type = T; };" |
| 267 | "template <class T, class U = T&> void g(typename identity<U>::type);" |
| 268 | "void f() { int x; g<int>(x); }"); |
| 269 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 270 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g<int>(x)")); |
| 271 | |
| 272 | AST = |
| 273 | tooling::buildASTFromCode("typedef int* IntPtr;" |
| 274 | "void g(IntPtr&); void f() { int* x; g(x); }"); |
| 275 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 276 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 277 | |
| 278 | AST = tooling::buildASTFromCode( |
| 279 | "typedef int* IntPtr; typedef IntPtr& IntPtrRef;" |
| 280 | "void g(IntPtrRef); void f() { int* x; g(x); }"); |
| 281 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 282 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 283 | |
| 284 | AST = tooling::buildASTFromCode( |
| 285 | "void f() { struct A {}; A operator+(A&, int); A x; x + 1; }"); |
| 286 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 287 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x + 1")); |
| 288 | |
| 289 | AST = tooling::buildASTFromCode( |
| 290 | "void f() { struct A { A(int&); }; int x; A y(x); }"); |
| 291 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 292 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 293 | |
| 294 | AST = tooling::buildASTFromCode( |
| 295 | "void f() { struct A { A(); A(A&); }; A x; A y(x); }"); |
| 296 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 297 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 298 | } |
| 299 | |
| 300 | TEST(ExprMutationAnalyzerTest, ByConstRefArgument) { |
| 301 | auto AST = tooling::buildASTFromCode( |
| 302 | "void g(const int&); void f() { int x; g(x); }"); |
| 303 | auto Results = |
| 304 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 305 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 306 | |
| 307 | AST = tooling::buildASTFromCode("typedef const int& CIntRef;" |
| 308 | "void g(CIntRef); void f() { int x; g(x); }"); |
| 309 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 310 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 311 | |
| 312 | AST = tooling::buildASTFromCode( |
| 313 | "template <class T> using CTRef = const T&;" |
| 314 | "void g(CTRef<int>); void f() { int x; g(x); }"); |
| 315 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 316 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 317 | |
| 318 | AST = tooling::buildASTFromCode( |
| 319 | "template <class T> struct identity { using type = T; };" |
| 320 | "template <class T, class U = const T&>" |
| 321 | "void g(typename identity<U>::type);" |
| 322 | "void f() { int x; g<int>(x); }"); |
| 323 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 324 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 325 | |
| 326 | AST = tooling::buildASTFromCode( |
| 327 | "void f() { struct A {}; A operator+(const A&, int); A x; x + 1; }"); |
| 328 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 329 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 330 | |
| 331 | AST = tooling::buildASTFromCode( |
| 332 | "void f() { struct A { A(const int&); }; int x; A y(x); }"); |
| 333 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 334 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 335 | |
| 336 | AST = tooling::buildASTFromCode( |
| 337 | "void f() { struct A { A(); A(const A&); }; A x; A y(x); }"); |
| 338 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 339 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 340 | } |
| 341 | |
| 342 | TEST(ExprMutationAnalyzerTest, ByNonConstRRefArgument) { |
| 343 | auto AST = tooling::buildASTFromCode( |
| 344 | "void g(int&&); void f() { int x; g(static_cast<int &&>(x)); }"); |
| 345 | auto Results = |
| 346 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 347 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 348 | ElementsAre("g(static_cast<int &&>(x))")); |
| 349 | |
| 350 | AST = tooling::buildASTFromCode( |
| 351 | "void f() { struct A {}; A operator+(A&&, int); " |
| 352 | "A x; static_cast<A &&>(x) + 1; }"); |
| 353 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 354 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 355 | ElementsAre("static_cast<A &&>(x) + 1")); |
| 356 | |
| 357 | AST = tooling::buildASTFromCode("void f() { struct A { A(int&&); }; " |
| 358 | "int x; A y(static_cast<int &&>(x)); }"); |
| 359 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 360 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 361 | ElementsAre("static_cast<int &&>(x)")); |
| 362 | |
| 363 | AST = tooling::buildASTFromCode("void f() { struct A { A(); A(A&&); }; " |
| 364 | "A x; A y(static_cast<A &&>(x)); }"); |
| 365 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 366 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 367 | ElementsAre("static_cast<A &&>(x)")); |
| 368 | } |
| 369 | |
| 370 | TEST(ExprMutationAnalyzerTest, ByConstRRefArgument) { |
| 371 | auto AST = tooling::buildASTFromCode( |
| 372 | "void g(const int&&); void f() { int x; g(static_cast<int&&>(x)); }"); |
| 373 | auto Results = |
| 374 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 375 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 376 | |
| 377 | AST = tooling::buildASTFromCode( |
| 378 | "void f() { struct A {}; A operator+(const A&&, int); " |
| 379 | "A x; static_cast<A&&>(x) + 1; }"); |
| 380 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 381 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 382 | |
| 383 | AST = tooling::buildASTFromCode("void f() { struct A { A(const int&&); }; " |
| 384 | "int x; A y(static_cast<int&&>(x)); }"); |
| 385 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 386 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 387 | |
| 388 | AST = tooling::buildASTFromCode("void f() { struct A { A(); A(const A&&); }; " |
| 389 | "A x; A y(static_cast<A&&>(x)); }"); |
| 390 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 391 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 392 | } |
| 393 | |
| 394 | TEST(ExprMutationAnalyzerTest, Move) { |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 395 | auto AST = |
| 396 | tooling::buildASTFromCode(StdRemoveReference + StdMove + |
| 397 | "void f() { struct A {}; A x; std::move(x); }"); |
| 398 | auto Results = |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 399 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 400 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 401 | |
| 402 | AST = tooling::buildASTFromCode( |
| 403 | StdRemoveReference + StdMove + |
| 404 | "void f() { struct A {}; A x, y; std::move(x) = y; }"); |
| 405 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 406 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("std::move(x) = y")); |
| 407 | |
| 408 | AST = tooling::buildASTFromCode(StdRemoveReference + StdMove + |
| 409 | "void f() { int x, y; y = std::move(x); }"); |
| 410 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 411 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 412 | |
| 413 | AST = tooling::buildASTFromCode( |
| 414 | StdRemoveReference + StdMove + |
| 415 | "struct S { S(); S(const S&); S& operator=(const S&); };" |
| 416 | "void f() { S x, y; y = std::move(x); }"); |
| 417 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 418 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 419 | |
| 420 | AST = |
| 421 | tooling::buildASTFromCode(StdRemoveReference + StdMove + |
| 422 | "struct S { S(); S(S&&); S& operator=(S&&); };" |
| 423 | "void f() { S x, y; y = std::move(x); }"); |
| 424 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 425 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)")); |
| 426 | |
| 427 | AST = |
| 428 | tooling::buildASTFromCode(StdRemoveReference + StdMove + |
| 429 | "struct S { S(); S(const S&); S(S&&);" |
| 430 | "S& operator=(const S&); S& operator=(S&&); };" |
| 431 | "void f() { S x, y; y = std::move(x); }"); |
| 432 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 433 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)")); |
| 434 | |
| 435 | AST = tooling::buildASTFromCode( |
| 436 | StdRemoveReference + StdMove + |
| 437 | "struct S { S(); S(const S&); S(S&&);" |
| 438 | "S& operator=(const S&); S& operator=(S&&); };" |
| 439 | "void f() { const S x; S y; y = std::move(x); }"); |
| 440 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 441 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 442 | |
| 443 | AST = tooling::buildASTFromCode(StdRemoveReference + StdMove + |
| 444 | "struct S { S(); S(S); S& operator=(S); };" |
| 445 | "void f() { S x, y; y = std::move(x); }"); |
| 446 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 447 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 448 | |
| 449 | AST = tooling::buildASTFromCode( |
| 450 | StdRemoveReference + StdMove + |
| 451 | "struct S{}; void f() { S x, y; y = std::move(x); }"); |
| 452 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 453 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("y = std::move(x)")); |
| 454 | |
| 455 | AST = tooling::buildASTFromCode( |
| 456 | StdRemoveReference + StdMove + |
| 457 | "struct S{}; void f() { const S x; S y; y = std::move(x); }"); |
| 458 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 459 | EXPECT_FALSE(isMutated(Results, AST.get())); |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST(ExprMutationAnalyzerTest, Forward) { |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 463 | auto AST = tooling::buildASTFromCode( |
| 464 | StdRemoveReference + StdForward + |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 465 | "void f() { struct A {}; A x; std::forward<A &>(x); }"); |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 466 | auto Results = |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 467 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 468 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 469 | |
| 470 | AST = tooling::buildASTFromCode( |
| 471 | StdRemoveReference + StdForward + |
| 472 | "void f() { struct A {}; A x, y; std::forward<A &>(x) = y; }"); |
| 473 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 474 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 475 | ElementsAre("std::forward<A &>(x) = y")); |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | TEST(ExprMutationAnalyzerTest, CallUnresolved) { |
| 479 | auto AST = tooling::buildASTFromCodeWithArgs( |
| 480 | "template <class T> void f() { T x; g(x); }", |
| 481 | {"-fno-delayed-template-parsing"}); |
| 482 | auto Results = |
| 483 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 484 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 485 | |
| 486 | AST = tooling::buildASTFromCodeWithArgs( |
| 487 | "template <int N> void f() { char x[N]; g(x); }", |
| 488 | {"-fno-delayed-template-parsing"}); |
| 489 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 490 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 491 | |
| 492 | AST = tooling::buildASTFromCodeWithArgs( |
| 493 | "template <class T> void f(T t) { int x; g(t, x); }", |
| 494 | {"-fno-delayed-template-parsing"}); |
| 495 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 496 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(t, x)")); |
| 497 | |
| 498 | AST = tooling::buildASTFromCodeWithArgs( |
| 499 | "template <class T> void f(T t) { int x; t.mf(x); }", |
| 500 | {"-fno-delayed-template-parsing"}); |
| 501 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 502 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("t.mf(x)")); |
| 503 | |
| 504 | AST = tooling::buildASTFromCodeWithArgs( |
| 505 | "template <class T> struct S;" |
| 506 | "template <class T> void f() { S<T> s; int x; s.mf(x); }", |
| 507 | {"-fno-delayed-template-parsing"}); |
| 508 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 509 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf(x)")); |
| 510 | |
| 511 | AST = tooling::buildASTFromCodeWithArgs( |
| 512 | "struct S { template <class T> void mf(); };" |
| 513 | "template <class T> void f(S s) { int x; s.mf<T>(x); }", |
| 514 | {"-fno-delayed-template-parsing"}); |
| 515 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 516 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("s.mf<T>(x)")); |
| 517 | |
| 518 | AST = tooling::buildASTFromCodeWithArgs("template <class F>" |
| 519 | "void g(F f) { int x; f(x); } ", |
| 520 | {"-fno-delayed-template-parsing"}); |
| 521 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 522 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("f(x)")); |
| 523 | |
| 524 | AST = tooling::buildASTFromCodeWithArgs( |
| 525 | "template <class T> void f() { int x; (void)T(x); }", |
| 526 | {"-fno-delayed-template-parsing"}); |
| 527 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 528 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("T(x)")); |
| 529 | } |
| 530 | |
| 531 | TEST(ExprMutationAnalyzerTest, ReturnAsValue) { |
| 532 | auto AST = tooling::buildASTFromCode("int f() { int x; return x; }"); |
| 533 | auto Results = |
| 534 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 535 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 536 | |
| 537 | AST = tooling::buildASTFromCode("int* f() { int* x; return x; }"); |
| 538 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 539 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 540 | |
| 541 | AST = tooling::buildASTFromCode("typedef int* IntPtr;" |
| 542 | "IntPtr f() { int* x; return x; }"); |
| 543 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 544 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 545 | } |
| 546 | |
| 547 | TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRef) { |
| 548 | const auto AST = tooling::buildASTFromCode("int& f() { int x; return x; }"); |
| 549 | const auto Results = |
| 550 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 551 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("return x;")); |
| 552 | } |
| 553 | |
| 554 | TEST(ExprMutationAnalyzerTest, ReturnAsConstRef) { |
| 555 | const auto AST = |
| 556 | tooling::buildASTFromCode("const int& f() { int x; return x; }"); |
| 557 | const auto Results = |
| 558 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 559 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 560 | } |
| 561 | |
| 562 | TEST(ExprMutationAnalyzerTest, ReturnAsNonConstRRef) { |
| 563 | const auto AST = tooling::buildASTFromCode( |
| 564 | "int&& f() { int x; return static_cast<int &&>(x); }"); |
| 565 | const auto Results = |
| 566 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 567 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 568 | ElementsAre("return static_cast<int &&>(x);")); |
| 569 | } |
| 570 | |
| 571 | TEST(ExprMutationAnalyzerTest, ReturnAsConstRRef) { |
| 572 | const auto AST = tooling::buildASTFromCode( |
| 573 | "const int&& f() { int x; return static_cast<int&&>(x); }"); |
| 574 | const auto Results = |
| 575 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 576 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 577 | } |
| 578 | |
| 579 | TEST(ExprMutationAnalyzerTest, TakeAddress) { |
| 580 | const auto AST = |
| 581 | tooling::buildASTFromCode("void g(int*); void f() { int x; g(&x); }"); |
| 582 | const auto Results = |
| 583 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 584 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("&x")); |
| 585 | } |
| 586 | |
| 587 | TEST(ExprMutationAnalyzerTest, ArrayToPointerDecay) { |
| 588 | const auto AST = |
| 589 | tooling::buildASTFromCode("void g(int*); void f() { int x[2]; g(x); }"); |
| 590 | const auto Results = |
| 591 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 592 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 593 | } |
| 594 | |
| 595 | TEST(ExprMutationAnalyzerTest, TemplateWithArrayToPointerDecay) { |
| 596 | const auto AST = tooling::buildASTFromCodeWithArgs( |
| 597 | "template <typename T> struct S { static constexpr int v = 8; };" |
| 598 | "template <> struct S<int> { static constexpr int v = 4; };" |
| 599 | "void g(char*);" |
| 600 | "template <typename T> void f() { char x[S<T>::v]; g(x); }" |
| 601 | "template <> void f<int>() { char y[S<int>::v]; g(y); }", |
| 602 | {"-fno-delayed-template-parsing"}); |
| 603 | const auto ResultsX = |
| 604 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 605 | EXPECT_THAT(mutatedBy(ResultsX, AST.get()), ElementsAre("g(x)")); |
| 606 | const auto ResultsY = |
| 607 | match(withEnclosingCompound(declRefTo("y")), AST->getASTContext()); |
| 608 | EXPECT_THAT(mutatedBy(ResultsY, AST.get()), ElementsAre("y")); |
| 609 | } |
| 610 | |
| 611 | TEST(ExprMutationAnalyzerTest, FollowRefModified) { |
| 612 | auto AST = tooling::buildASTFromCode( |
| 613 | "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; " |
| 614 | "int& r3 = r2; r3 = 10; }"); |
| 615 | auto Results = |
| 616 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 617 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 618 | ElementsAre("r0", "r1", "r2", "r3", "r3 = 10")); |
| 619 | |
| 620 | AST = tooling::buildASTFromCode( |
| 621 | "typedef int& IntRefX;" |
| 622 | "using IntRefY = int&;" |
| 623 | "void f() { int x; IntRefX r0 = x; IntRefY r1 = r0;" |
| 624 | "decltype((x)) r2 = r1; r2 = 10; }"); |
| 625 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 626 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 627 | ElementsAre("r0", "r1", "r2", "r2 = 10")); |
| 628 | } |
| 629 | |
| 630 | TEST(ExprMutationAnalyzerTest, FollowRefNotModified) { |
| 631 | auto AST = tooling::buildASTFromCode( |
| 632 | "void f() { int x; int& r0 = x; int& r1 = r0; int& r2 = r1; " |
| 633 | "int& r3 = r2; int& r4 = r3; int& r5 = r4;}"); |
| 634 | auto Results = |
| 635 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 636 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 637 | |
| 638 | AST = tooling::buildASTFromCode( |
| 639 | "void f() { int x; int& r0 = x; const int& r1 = r0;}"); |
| 640 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 641 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 642 | |
| 643 | AST = tooling::buildASTFromCode( |
| 644 | "typedef const int& CIntRefX;" |
| 645 | "using CIntRefY = const int&;" |
| 646 | "void f() { int x; int& r0 = x; CIntRefX r1 = r0;" |
| 647 | "CIntRefY r2 = r1; decltype((r1)) r3 = r2;}"); |
| 648 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 649 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 650 | } |
| 651 | |
| 652 | TEST(ExprMutationAnalyzerTest, FollowConditionalRefModified) { |
| 653 | const auto AST = tooling::buildASTFromCode( |
| 654 | "void f() { int x, y; bool b; int &r = b ? x : y; r = 10; }"); |
| 655 | const auto Results = |
| 656 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 657 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("r", "r = 10")); |
| 658 | } |
| 659 | |
| 660 | TEST(ExprMutationAnalyzerTest, FollowConditionalRefNotModified) { |
| 661 | const auto AST = tooling::buildASTFromCode( |
| 662 | "void f() { int x, y; bool b; int& r = b ? x : y; }"); |
| 663 | const auto Results = |
| 664 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 665 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 666 | } |
| 667 | |
Shuai Wang | cb98b70 | 2018-09-14 20:07:18 +0000 | [diff] [blame] | 668 | TEST(ExprMutationAnalyzerTest, FollowFuncArgModified) { |
| 669 | auto AST = |
| 670 | tooling::buildASTFromCode("template <class T> void g(T&& t) { t = 10; }" |
| 671 | "void f() { int x; g(x); }"); |
| 672 | auto Results = |
| 673 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 674 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 675 | |
| 676 | AST = tooling::buildASTFromCode( |
| 677 | "void h(int&);" |
| 678 | "template <class... Args> void g(Args&&... args) { h(args...); }" |
| 679 | "void f() { int x; g(x); }"); |
| 680 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 681 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
| 682 | |
| 683 | AST = tooling::buildASTFromCode( |
| 684 | "void h(int&, int);" |
| 685 | "template <class... Args> void g(Args&&... args) { h(args...); }" |
| 686 | "void f() { int x, y; g(x, y); }"); |
| 687 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 688 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x, y)")); |
| 689 | Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext()); |
| 690 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 691 | |
| 692 | AST = tooling::buildASTFromCode( |
| 693 | "void h(int, int&);" |
| 694 | "template <class... Args> void g(Args&&... args) { h(args...); }" |
| 695 | "void f() { int x, y; g(y, x); }"); |
| 696 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 697 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(y, x)")); |
| 698 | Results = match(withEnclosingCompound(declRefTo("y")), AST->getASTContext()); |
| 699 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 700 | |
| 701 | AST = tooling::buildASTFromCode( |
| 702 | "struct S { template <class T> S(T&& t) { t = 10; } };" |
| 703 | "void f() { int x; S s(x); }"); |
| 704 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 705 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 706 | |
| 707 | AST = tooling::buildASTFromCode( |
| 708 | "struct S { template <class T> S(T&& t) : m(++t) { } int m; };" |
| 709 | "void f() { int x; S s(x); }"); |
| 710 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 711 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 712 | |
| 713 | AST = tooling::buildASTFromCode( |
| 714 | StdRemoveReference + StdForward + |
| 715 | "template <class... Args> void u(Args&...);" |
| 716 | "template <class... Args> void h(Args&&... args)" |
| 717 | "{ u(std::forward<Args>(args)...); }" |
| 718 | "template <class... Args> void g(Args&&... args)" |
| 719 | "{ h(std::forward<Args>(args)...); }" |
| 720 | "void f() { int x; g(x); }"); |
| 721 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 722 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("g(x)")); |
Shuai Wang | cb98b70 | 2018-09-14 20:07:18 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | TEST(ExprMutationAnalyzerTest, FollowFuncArgNotModified) { |
| 726 | auto AST = tooling::buildASTFromCode("template <class T> void g(T&&) {}" |
| 727 | "void f() { int x; g(x); }"); |
| 728 | auto Results = |
| 729 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 730 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 731 | |
| 732 | AST = tooling::buildASTFromCode("template <class T> void g(T&& t) { t; }" |
| 733 | "void f() { int x; g(x); }"); |
| 734 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 735 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 736 | |
| 737 | AST = |
| 738 | tooling::buildASTFromCode("template <class... Args> void g(Args&&...) {}" |
| 739 | "void f() { int x; g(x); }"); |
| 740 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 741 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 742 | |
| 743 | AST = |
| 744 | tooling::buildASTFromCode("template <class... Args> void g(Args&&...) {}" |
| 745 | "void f() { int y, x; g(y, x); }"); |
| 746 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 747 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 748 | |
| 749 | AST = tooling::buildASTFromCode( |
| 750 | "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 | |
| 756 | AST = tooling::buildASTFromCode( |
| 757 | "struct S { template <class T> S(T&& t) { t; } };" |
| 758 | "void f() { int x; S s(x); }"); |
| 759 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 760 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 761 | |
| 762 | AST = tooling::buildASTFromCode( |
| 763 | "struct S { template <class T> S(T&& t) : m(t) { } int m; };" |
| 764 | "void f() { int x; S s(x); }"); |
| 765 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 766 | EXPECT_FALSE(isMutated(Results, AST.get())); |
Shuai Wang | 4305993 | 2018-09-17 20:10:56 +0000 | [diff] [blame] | 767 | |
| 768 | AST = tooling::buildASTFromCode( |
| 769 | StdRemoveReference + StdForward + |
| 770 | "template <class... Args> void u(Args...);" |
| 771 | "template <class... Args> void h(Args&&... args)" |
| 772 | "{ u(std::forward<Args>(args)...); }" |
| 773 | "template <class... Args> void g(Args&&... args)" |
| 774 | "{ h(std::forward<Args>(args)...); }" |
| 775 | "void f() { int x; g(x); }"); |
| 776 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 777 | EXPECT_FALSE(isMutated(Results, AST.get())); |
Shuai Wang | cb98b70 | 2018-09-14 20:07:18 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Shuai Wang | e9192f8 | 2018-09-11 21:13:20 +0000 | [diff] [blame] | 780 | TEST(ExprMutationAnalyzerTest, ArrayElementModified) { |
| 781 | const auto AST = |
| 782 | tooling::buildASTFromCode("void f() { int x[2]; x[0] = 10; }"); |
| 783 | const auto Results = |
| 784 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 785 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x[0] = 10")); |
| 786 | } |
| 787 | |
| 788 | TEST(ExprMutationAnalyzerTest, ArrayElementNotModified) { |
| 789 | const auto AST = tooling::buildASTFromCode("void f() { int x[2]; x[0]; }"); |
| 790 | const auto Results = |
| 791 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 792 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 793 | } |
| 794 | |
| 795 | TEST(ExprMutationAnalyzerTest, NestedMemberModified) { |
| 796 | auto AST = tooling::buildASTFromCode( |
| 797 | "void f() { struct A { int vi; }; struct B { A va; }; " |
| 798 | "struct C { B vb; }; C x; x.vb.va.vi = 10; }"); |
| 799 | auto Results = |
| 800 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 801 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.vb.va.vi = 10")); |
| 802 | |
| 803 | AST = tooling::buildASTFromCodeWithArgs( |
| 804 | "template <class T> void f() { T x; x.y.z = 10; }", |
| 805 | {"-fno-delayed-template-parsing"}); |
| 806 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 807 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10")); |
| 808 | |
| 809 | AST = tooling::buildASTFromCodeWithArgs( |
| 810 | "template <class T> struct S;" |
| 811 | "template <class T> void f() { S<T> x; x.y.z = 10; }", |
| 812 | {"-fno-delayed-template-parsing"}); |
| 813 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 814 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.y.z = 10")); |
| 815 | } |
| 816 | |
| 817 | TEST(ExprMutationAnalyzerTest, NestedMemberNotModified) { |
| 818 | auto AST = tooling::buildASTFromCode( |
| 819 | "void f() { struct A { int vi; }; struct B { A va; }; " |
| 820 | "struct C { B vb; }; C x; x.vb.va.vi; }"); |
| 821 | auto Results = |
| 822 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 823 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 824 | |
| 825 | AST = tooling::buildASTFromCodeWithArgs( |
| 826 | "template <class T> void f() { T x; x.y.z; }", |
| 827 | {"-fno-delayed-template-parsing"}); |
| 828 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 829 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 830 | |
| 831 | AST = tooling::buildASTFromCodeWithArgs( |
| 832 | "template <class T> struct S;" |
| 833 | "template <class T> void f() { S<T> x; x.y.z; }", |
| 834 | {"-fno-delayed-template-parsing"}); |
| 835 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 836 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 837 | } |
| 838 | |
| 839 | TEST(ExprMutationAnalyzerTest, CastToValue) { |
| 840 | const auto AST = |
| 841 | tooling::buildASTFromCode("void f() { int x; static_cast<double>(x); }"); |
| 842 | const auto Results = |
| 843 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 844 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 845 | } |
| 846 | |
| 847 | TEST(ExprMutationAnalyzerTest, CastToRefModified) { |
| 848 | auto AST = tooling::buildASTFromCode( |
| 849 | "void f() { int x; static_cast<int &>(x) = 10; }"); |
| 850 | auto Results = |
| 851 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 852 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 853 | ElementsAre("static_cast<int &>(x) = 10")); |
| 854 | |
| 855 | AST = tooling::buildASTFromCode( |
| 856 | "typedef int& IntRef;" |
| 857 | "void f() { int x; static_cast<IntRef>(x) = 10; }"); |
| 858 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 859 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 860 | ElementsAre("static_cast<IntRef>(x) = 10")); |
| 861 | } |
| 862 | |
| 863 | TEST(ExprMutationAnalyzerTest, CastToRefNotModified) { |
| 864 | const auto AST = |
| 865 | tooling::buildASTFromCode("void f() { int x; static_cast<int&>(x); }"); |
| 866 | const auto Results = |
| 867 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 868 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 869 | } |
| 870 | |
| 871 | TEST(ExprMutationAnalyzerTest, CastToConstRef) { |
| 872 | auto AST = tooling::buildASTFromCode( |
| 873 | "void f() { int x; static_cast<const int&>(x); }"); |
| 874 | auto Results = |
| 875 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 876 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 877 | |
| 878 | AST = |
| 879 | tooling::buildASTFromCode("typedef const int& CIntRef;" |
| 880 | "void f() { int x; static_cast<CIntRef>(x); }"); |
| 881 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 882 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 883 | } |
| 884 | |
| 885 | TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByValue) { |
| 886 | const auto AST = |
| 887 | tooling::buildASTFromCode("void f() { int x; [=]() { x = 10; }; }"); |
| 888 | const auto Results = |
| 889 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 890 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 891 | } |
| 892 | |
| 893 | TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByValue) { |
| 894 | const auto AST = |
| 895 | tooling::buildASTFromCode("void f() { int x; [x]() { x = 10; }; }"); |
| 896 | const auto Results = |
| 897 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 898 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 899 | } |
| 900 | |
| 901 | TEST(ExprMutationAnalyzerTest, LambdaDefaultCaptureByRef) { |
| 902 | const auto AST = |
| 903 | tooling::buildASTFromCode("void f() { int x; [&]() { x = 10; }; }"); |
| 904 | const auto Results = |
| 905 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 906 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 907 | ElementsAre(ResultOf(removeSpace, "[&](){x=10;}"))); |
| 908 | } |
| 909 | |
| 910 | TEST(ExprMutationAnalyzerTest, LambdaExplicitCaptureByRef) { |
| 911 | const auto AST = |
| 912 | tooling::buildASTFromCode("void f() { int x; [&x]() { x = 10; }; }"); |
| 913 | const auto Results = |
| 914 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 915 | EXPECT_THAT(mutatedBy(Results, AST.get()), |
| 916 | ElementsAre(ResultOf(removeSpace, "[&x](){x=10;}"))); |
| 917 | } |
| 918 | |
| 919 | TEST(ExprMutationAnalyzerTest, RangeForArrayByRefModified) { |
| 920 | auto AST = tooling::buildASTFromCode( |
| 921 | "void f() { int x[2]; for (int& e : x) e = 10; }"); |
| 922 | auto Results = |
| 923 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 924 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10")); |
| 925 | |
| 926 | AST = tooling::buildASTFromCode( |
| 927 | "typedef int& IntRef;" |
| 928 | "void f() { int x[2]; for (IntRef e : x) e = 10; }"); |
| 929 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 930 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10")); |
| 931 | } |
| 932 | |
| 933 | TEST(ExprMutationAnalyzerTest, RangeForArrayByRefNotModified) { |
| 934 | const auto AST = |
| 935 | tooling::buildASTFromCode("void f() { int x[2]; for (int& e : x) e; }"); |
| 936 | const auto Results = |
| 937 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 938 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 939 | } |
| 940 | |
| 941 | TEST(ExprMutationAnalyzerTest, RangeForArrayByValue) { |
| 942 | auto AST = tooling::buildASTFromCode( |
| 943 | "void f() { int x[2]; for (int e : x) e = 10; }"); |
| 944 | auto Results = |
| 945 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 946 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 947 | |
| 948 | AST = tooling::buildASTFromCode( |
| 949 | "void f() { int* x[2]; for (int* e : x) e = nullptr; }"); |
| 950 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 951 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 952 | |
| 953 | AST = tooling::buildASTFromCode( |
| 954 | "typedef int* IntPtr;" |
| 955 | "void f() { int* x[2]; for (IntPtr e : x) e = nullptr; }"); |
| 956 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 957 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 958 | } |
| 959 | |
| 960 | TEST(ExprMutationAnalyzerTest, RangeForArrayByConstRef) { |
| 961 | auto AST = tooling::buildASTFromCode( |
| 962 | "void f() { int x[2]; for (const int& e : x) e; }"); |
| 963 | auto Results = |
| 964 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 965 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 966 | |
| 967 | AST = tooling::buildASTFromCode( |
| 968 | "typedef const int& CIntRef;" |
| 969 | "void f() { int x[2]; for (CIntRef e : x) e; }"); |
| 970 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 971 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 972 | } |
| 973 | |
| 974 | TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefModified) { |
| 975 | const auto AST = |
| 976 | tooling::buildASTFromCode("struct V { int* begin(); int* end(); };" |
| 977 | "void f() { V x; for (int& e : x) e = 10; }"); |
| 978 | const auto Results = |
| 979 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 980 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("e", "e = 10")); |
| 981 | } |
| 982 | |
| 983 | TEST(ExprMutationAnalyzerTest, RangeForNonArrayByRefNotModified) { |
| 984 | const auto AST = |
| 985 | tooling::buildASTFromCode("struct V { int* begin(); int* end(); };" |
| 986 | "void f() { V x; for (int& e : x) e; }"); |
| 987 | const auto Results = |
| 988 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 989 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 990 | } |
| 991 | |
| 992 | TEST(ExprMutationAnalyzerTest, RangeForNonArrayByValue) { |
| 993 | const auto AST = tooling::buildASTFromCode( |
| 994 | "struct V { const int* begin() const; const int* end() const; };" |
| 995 | "void f() { V x; for (int e : x) e; }"); |
| 996 | const auto Results = |
| 997 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 998 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 999 | } |
| 1000 | |
| 1001 | TEST(ExprMutationAnalyzerTest, RangeForNonArrayByConstRef) { |
| 1002 | const auto AST = tooling::buildASTFromCode( |
| 1003 | "struct V { const int* begin() const; const int* end() const; };" |
| 1004 | "void f() { V x; for (const int& e : x) e; }"); |
| 1005 | const auto Results = |
| 1006 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1007 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1008 | } |
| 1009 | |
| 1010 | TEST(ExprMutationAnalyzerTest, UnevaluatedExpressions) { |
| 1011 | auto AST = tooling::buildASTFromCode( |
| 1012 | "void f() { int x, y; decltype(x = 10) z = y; }"); |
| 1013 | auto Results = |
| 1014 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1015 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1016 | |
| 1017 | AST = tooling::buildASTFromCode( |
| 1018 | "void f() { int x, y; __typeof(x = 10) z = y; }"); |
| 1019 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1020 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1021 | |
| 1022 | AST = tooling::buildASTFromCode( |
| 1023 | "void f() { int x, y; __typeof__(x = 10) z = y; }"); |
| 1024 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1025 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1026 | |
| 1027 | AST = tooling::buildASTFromCode("void f() { int x; sizeof(x = 10); }"); |
| 1028 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1029 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1030 | |
| 1031 | AST = tooling::buildASTFromCode("void f() { int x; alignof(x = 10); }"); |
| 1032 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1033 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1034 | |
| 1035 | AST = tooling::buildASTFromCode("void f() { int x; noexcept(x = 10); }"); |
| 1036 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1037 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1038 | |
| 1039 | AST = tooling::buildASTFromCodeWithArgs("namespace std { class type_info; }" |
| 1040 | "void f() { int x; typeid(x = 10); }", |
| 1041 | {"-frtti"}); |
| 1042 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1043 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1044 | |
| 1045 | AST = tooling::buildASTFromCode( |
| 1046 | "void f() { int x; _Generic(x = 10, int: 0, default: 1); }"); |
| 1047 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1048 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1049 | } |
| 1050 | |
| 1051 | TEST(ExprMutationAnalyzerTest, NotUnevaluatedExpressions) { |
| 1052 | auto AST = tooling::buildASTFromCode("void f() { int x; sizeof(int[x++]); }"); |
| 1053 | auto Results = |
| 1054 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1055 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x++")); |
| 1056 | |
| 1057 | AST = tooling::buildASTFromCodeWithArgs( |
| 1058 | "namespace std { class type_info; }" |
| 1059 | "struct A { virtual ~A(); }; struct B : A {};" |
| 1060 | "struct X { A& f(); }; void f() { X x; typeid(x.f()); }", |
| 1061 | {"-frtti"}); |
| 1062 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1063 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x.f()")); |
| 1064 | } |
| 1065 | |
| 1066 | TEST(ExprMutationAnalyzerTest, UniquePtr) { |
| 1067 | const std::string UniquePtrDef = |
| 1068 | "template <class T> struct UniquePtr {" |
| 1069 | " UniquePtr();" |
| 1070 | " UniquePtr(const UniquePtr&) = delete;" |
| 1071 | " UniquePtr(UniquePtr&&);" |
| 1072 | " UniquePtr& operator=(const UniquePtr&) = delete;" |
| 1073 | " UniquePtr& operator=(UniquePtr&&);" |
| 1074 | " T& operator*() const;" |
| 1075 | " T* operator->() const;" |
| 1076 | "};"; |
| 1077 | |
| 1078 | auto AST = tooling::buildASTFromCode( |
| 1079 | UniquePtrDef + "void f() { UniquePtr<int> x; *x = 10; }"); |
| 1080 | auto Results = |
| 1081 | match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1082 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("* x = 10")); |
| 1083 | |
| 1084 | AST = tooling::buildASTFromCode(UniquePtrDef + |
| 1085 | "void f() { UniquePtr<int> x; *x; }"); |
| 1086 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1087 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1088 | |
| 1089 | AST = tooling::buildASTFromCode(UniquePtrDef + |
| 1090 | "void f() { UniquePtr<const int> x; *x; }"); |
| 1091 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1092 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1093 | |
| 1094 | AST = tooling::buildASTFromCode(UniquePtrDef + |
| 1095 | "struct S { int v; };" |
| 1096 | "void f() { UniquePtr<S> x; x->v; }"); |
| 1097 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1098 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 1099 | |
| 1100 | AST = tooling::buildASTFromCode(UniquePtrDef + |
| 1101 | "struct S { int v; };" |
| 1102 | "void f() { UniquePtr<const S> x; x->v; }"); |
| 1103 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1104 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1105 | |
| 1106 | AST = tooling::buildASTFromCode(UniquePtrDef + |
| 1107 | "struct S { void mf(); };" |
| 1108 | "void f() { UniquePtr<S> x; x->mf(); }"); |
| 1109 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1110 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); |
| 1111 | |
| 1112 | AST = tooling::buildASTFromCode( |
| 1113 | UniquePtrDef + "struct S { void mf() const; };" |
| 1114 | "void f() { UniquePtr<const S> x; x->mf(); }"); |
| 1115 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1116 | EXPECT_FALSE(isMutated(Results, AST.get())); |
| 1117 | |
| 1118 | AST = tooling::buildASTFromCodeWithArgs( |
| 1119 | UniquePtrDef + "template <class T> void f() { UniquePtr<T> x; x->mf(); }", |
| 1120 | {"-fno-delayed-template-parsing"}); |
| 1121 | Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); |
| 1122 | EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x->mf()")); |
| 1123 | } |
| 1124 | |
| 1125 | } // namespace clang |