| // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| // Exceptions. See /LICENSE for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| |
| // Tests that execution order is taken into account correctly. |
| |
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "lifetime_analysis/test/lifetime_analysis_test.h" |
| |
| namespace clang { |
| namespace tidy { |
| namespace lifetimes { |
| namespace { |
| |
| TEST_F(LifetimeAnalysisTest, OrderOfOperations_OrderOfExecution) { |
| // This is a regression test for a wrong result that was generated by the |
| // constraint-based approach. |
| |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* p) { |
| int* p2 = p; |
| int local = 42; |
| p = &local; |
| return p2; |
| } |
| )"), |
| LifetimesAre({{"target", "a -> a"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, OrderOfOperations_OrderOfExecution2) { |
| // This is a regression test for a wrong result that was generated by the |
| // constraint-based approach. |
| |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* p) { |
| int **pp = &p; |
| int local = 42; |
| int* p2; |
| pp = &p2; |
| *pp = &local; |
| return p; |
| } |
| )"), |
| LifetimesAre({{"target", "a -> a"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, OrderOfOperations_Assignment) { |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* p) { |
| int* p2 = p; |
| int local = 42; |
| p2 = &local; |
| p2 = p; |
| return p2; |
| } |
| )"), |
| LifetimesAre({{"target", "a -> a"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, TakeAReferenceLater) { |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* p) { |
| int* p2 = p; |
| int local = 42; |
| p = &local; |
| int** pp = &p; |
| return p2; |
| } |
| )"), |
| LifetimesAre({{"target", "a -> a"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, ChainedAssignment) { |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* a, int* b, int* c) { |
| a = b = c; |
| return a; |
| } |
| )"), |
| LifetimesAre({{"target", "a, b, c -> c"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, ParenExpr) { |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* a, int* b, int* c) { |
| a = (b = c); |
| return a; |
| } |
| )"), |
| LifetimesAre({{"target", "a, b, c -> c"}})); |
| } |
| |
| TEST_F(LifetimeAnalysisTest, ParenExpr2) { |
| EXPECT_THAT(GetLifetimes(R"( |
| int* target(int* a, int* b, int* c) { |
| (a = b) = c; |
| return a; |
| } |
| )"), |
| LifetimesAre({{"target", "a, b, c -> c"}})); |
| } |
| |
| } // namespace |
| } // namespace lifetimes |
| } // namespace tidy |
| } // namespace clang |