Skip to content

Commit 4d457b8

Browse files
committed
DeadCode: Add macro/template tests
Ensure that macro expansions and multiple instances of code work together.
1 parent aca6d40 commit 4d457b8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

cpp/common/test/rules/deadcode/DeadCode.expected

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
| test.cpp:85:3:85:43 | declaration | This statement is dead code. |
1616
| test.cpp:87:3:87:30 | declaration | This statement is dead code. |
1717
| test.cpp:90:3:90:50 | declaration | This statement is dead code. |
18-
| test.cpp:108:3:108:21 | ExprStmt | This statement is dead code. |
19-
| test.cpp:120:3:120:23 | ExprStmt | This statement is dead code. |
18+
| test.cpp:116:3:116:21 | ExprStmt | This statement is dead code. |
19+
| test.cpp:117:3:117:27 | ExprStmt | This statement is dead code. |
20+
| test.cpp:118:7:118:32 | ExprStmt | This statement is dead code. |
21+
| test.cpp:139:3:139:35 | ExprStmt | This statement is dead code. |

cpp/common/test/rules/deadcode/test.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,27 @@ class Baz {
102102
void bar() {} // No side effects
103103
};
104104

105+
#define FULL_STMT_NO_SIDE_EFFECTS no_side_effects(1);
106+
#define PART_STMT_NO_SIDE_EFFECTS no_side_effects(1)
107+
#define BLOCK_SOME_SIDE_EFFECTS \
108+
{ \
109+
may_have_side_effects(); \
110+
no_side_effects(1); \
111+
}
112+
105113
template <typename T> void test_template() {
106114
T t;
107-
t.bar(); // COMPLIANT
108-
no_side_effects(1); // NON_COMPLIANT
115+
t.bar(); // COMPLIANT
116+
no_side_effects(1); // NON_COMPLIANT
117+
FULL_STMT_NO_SIDE_EFFECTS // NON_COMPLIANT
118+
PART_STMT_NO_SIDE_EFFECTS; // NON_COMPLIANT
119+
BLOCK_SOME_SIDE_EFFECTS; // COMPLIANT - cannot determine loc for
120+
// no_side_effects(1)
121+
}
122+
123+
template <typename T> void test_variant_side_effects() {
124+
T t;
125+
t.bar(); // COMPLIANT - not dead in at least one instance
109126
}
110127

111128
template <typename T> void test_unused_template() {
@@ -117,5 +134,8 @@ template <typename T> void test_unused_template() {
117134

118135
void test() {
119136
test_template<Foo>();
120-
test_template<Baz>(); // NON_COMPLIANT - template call has no affect
137+
test_template<Baz>();
138+
test_variant_side_effects<Foo>(); // COMPLIANT
139+
test_variant_side_effects<Baz>(); // NON_COMPLIANT - no effect in this
140+
// instantiation
121141
}

0 commit comments

Comments
 (0)