Skip to content

Commit 0caf0f1

Browse files
authored
Merge pull request #430 from geoffw0/exprtemplate
CPP: Exclude template code from ExprHasNoEffect.ql
2 parents 0e9c7fc + 09782d1 commit 0caf0f1

10 files changed

Lines changed: 48 additions & 22 deletions

File tree

change-notes/1.19/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| **Query** | **Expected impact** | **Change** |
1717
|----------------------------|------------------------|------------------------------------------------------------------|
1818
| Empty branch of conditional | Fewer false positive results | The query now recognizes commented blocks more reliably. |
19+
| Expression has no effect | Fewer false positive results | Expressions in template instantiations are now excluded from this query. |
1920
| Resource not released in destructor | Fewer false positive results | Placement new is now excluded from the query. Also fixed an issue where false positives could occur if the destructor body was not in the snapshot. |
2021
| Missing return statement (`cpp/missing-return`) | Visible by default | The precision of this query has been increased from 'medium' to 'high', which makes it visible by default in LGTM. It was 'medium' in release 1.17 and 1.18 because it had false positives due to an extractor bug that was fixed in 1.18. |
2122
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |

cpp/ql/src/Likely Bugs/Likely Typos/ExprHasNoEffect.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ where // EQExprs are covered by CompareWhereAssignMeant.ql
100100
not accessInInitOfForStmt(peivc) and
101101
not peivc.isCompilerGenerated() and
102102
not exists(Macro m | peivc = m.getAnInvocation().getAnExpandedElement()) and
103+
not peivc.isFromTemplateInstantiation(_) and
103104
parent = peivc.getParent() and
104105
not parent.isInMacroExpansion() and
105106
not parent instanceof PureExprInVoidContext and

cpp/ql/test/library-tests/queries/expr_has_no_effect/calls.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ int external();
55
class Base {
66
public:
77
virtual int thingy() {
8-
1;
8+
1; // BAD
99
}
1010

1111
int our_thingy() {
12-
Base::thingy();
12+
Base::thingy(); // BAD
1313
return 2;
1414
}
1515
};
1616

1717
class Derived : public Base {
1818
public:
1919
virtual int thingy() {
20-
external();
20+
external(); // GOOD
2121
return 3;
2222
}
2323
};
2424

2525
void internal() {
2626
Base* ptr = new Derived();
27-
ptr->thingy();
27+
ptr->thingy(); // GOOD
2828
}
2929

3030
}

cpp/ql/test/library-tests/queries/expr_has_no_effect/expr_has_no_effect.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
| calls.cpp:8:5:8:5 | 1 | This expression has no effect. | calls.cpp:8:5:8:5 | 1 | |
22
| calls.cpp:12:5:12:16 | call to thingy | This expression has no effect (because $@ has no external side effects). | calls.cpp:7:15:7:20 | thingy | thingy |
3-
| templatey.cpp:4:3:4:8 | ... << ... | This expression has no effect. | templatey.cpp:4:3:4:8 | ... << ... | |
43
| templatey.cpp:39:3:39:23 | call to pointless_add_numbers | This expression has no effect (because $@ has no external side effects). | templatey.cpp:29:5:29:25 | pointless_add_numbers | pointless_add_numbers |
54
| volatile.c:9:5:9:5 | c | This expression has no effect. | volatile.c:9:5:9:5 | c | |
65
| volatile.c:12:5:12:9 | access to array | This expression has no effect. | volatile.c:12:5:12:9 | access to array | |

cpp/ql/test/library-tests/queries/expr_has_no_effect/templatey.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
template <typename T>
22
void foo(T x, T y)
33
{
4-
x << y;
4+
x << y; // GOOD (effect depends on T)
55
};
66

77
struct streamable
@@ -15,9 +15,9 @@ void operator<<(streamable& lhs, streamable& rhs)
1515
int main()
1616
{
1717
int x = 3;
18-
foo(x, x);
18+
foo(x, x); // BAD [NOT DETECTED]
1919
streamable y;
20-
foo(y, y);
20+
foo(y, y); // BAD [NOT DETECTED]
2121
return 0;
2222
}
2323

@@ -34,7 +34,7 @@ int pointless_add_numbers(int lhs, int rhs)
3434
void call_add_numbers()
3535
{
3636
int accum = 0;
37-
add_numbers(accum, 4);
38-
add_numbers(accum, 10);
39-
pointless_add_numbers(accum, 20);
37+
add_numbers(accum, 4); // GOOD
38+
add_numbers(accum, 10); // GOOD
39+
pointless_add_numbers(accum, 20); // BAD
4040
}

cpp/ql/test/library-tests/queries/expr_has_no_effect/volatile.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ char *pc;
66
volatile char *pv;
77

88
void f(void) {
9-
c;
10-
v;
9+
c; // BAD
10+
v; // (accesses to volatile variables are considered impure)
1111

12-
pc[5];
12+
pc[5]; // BAD
1313
pv[5];
1414
((volatile char *)pc)[5];
1515

16-
*pc;
16+
*pc; // BAD
1717
*pv;
1818
*((volatile char *)pc);
1919

20-
*(pc + 5);
20+
*(pc + 5); // BAD
2121
*(pv + 5);
2222
*((volatile char *)(pc + 5));
2323
*(((volatile char *)pc) + 5);

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/ExprHasNoEffect/ExprHasNoEffect.expected

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| preproc.c:89:2:89:4 | call to fn4 | This expression has no effect (because $@ has no external side effects). | preproc.c:33:5:33:7 | fn4 | fn4 |
22
| preproc.c:94:2:94:4 | call to fn9 | This expression has no effect (because $@ has no external side effects). | preproc.c:78:5:78:7 | fn9 | fn9 |
3+
| template.cpp:19:3:19:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | template.cpp:9:10:9:19 | operator++ | operator++ |
34
| test.c:7:5:7:5 | 0 | This expression has no effect. | test.c:7:5:7:5 | 0 | |
45
| test.c:9:8:9:8 | 1 | This expression has no effect. | test.c:9:8:9:8 | 1 | |
56
| test.c:9:11:9:11 | 2 | This expression has no effect. | test.c:9:11:9:11 | 2 | |
@@ -19,11 +20,6 @@
1920
| test.c:26:15:26:16 | 32 | This expression has no effect. | test.c:26:15:26:16 | 32 | |
2021
| test.c:27:9:27:10 | 33 | This expression has no effect. | test.c:27:9:27:10 | 33 | |
2122
| test.cpp:24:3:24:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
22-
| test.cpp:24:3:24:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
23-
| test.cpp:24:3:24:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
24-
| test.cpp:25:3:25:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
25-
| test.cpp:25:3:25:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
2623
| test.cpp:25:3:25:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
27-
| test.cpp:26:3:26:3 | call to operator++ | This expression has no effect (because $@ has no external side effects). | test.cpp:9:14:9:23 | operator++ | operator++ |
2824
| test.cpp:62:5:62:5 | call to operator= | This expression has no effect (because $@ has no external side effects). | test.cpp:47:14:47:22 | operator= | operator= |
2925
| test.cpp:65:5:65:5 | call to operator= | This expression has no effect (because $@ has no external side effects). | test.cpp:55:7:55:7 | operator= | operator= |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#define UNUSED(x) (x)
3+
4+
void test2(int param)
5+
{
6+
UNUSED(param); // GOOD
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
template<class T>
3+
void Increment(T &t) {
4+
t++; // GOOD (sometimes has an effect)
5+
}
6+
7+
class Nothing {
8+
public:
9+
Nothing operator++(int) {
10+
return *this; // do nothing
11+
}
12+
};
13+
14+
void myTemplateTest() {
15+
int i = 0;
16+
Nothing n;
17+
18+
i++; // GOOD (always has an effect)
19+
n++; // BAD (never has an effect)
20+
Increment(i);
21+
Increment(n);
22+
}

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/ExprHasNoEffect/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MyTemplateClass
2323

2424
++arg1; // pure, does nothing
2525
++arg2; // pure, does nothing
26-
++arg3; // not pure in all cases (when _It is int this has a side-effect) [FALSE POSITIVE]
26+
++arg3; // not pure in all cases (when _It is int this has a side-effect)
2727

2828
return arg2;
2929
}

0 commit comments

Comments
 (0)