Skip to content

Commit 891bc34

Browse files
committed
C++: Fix another implicit/explicit this FP
1 parent 28bd591 commit 891bc34

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,35 @@
1111
import cpp
1212
import semmle.code.cpp.commons.Exclusions
1313

14+
/**
15+
* Holds if this is an implicit `this`.
16+
*
17+
* ThisExpr.isCompilerGenerated() is currently not being extracted, so use a heuristic.
18+
*/
19+
predicate isCompilerGenerated(ThisExpr te) {
20+
exists(
21+
string filepath, int line, int colStart, int colEnd, boolean zeroDiff, boolean sameLocAsCall
22+
|
23+
te.getLocation().hasLocationInfo(filepath, line, colStart, line, colEnd) and
24+
(if colStart = colEnd then zeroDiff = true else zeroDiff = false) and
25+
(
26+
if exists(Call c | c.getQualifier() = te | c.getLocation() = te.getLocation())
27+
then sameLocAsCall = true
28+
else sameLocAsCall = false
29+
)
30+
|
31+
zeroDiff = true
32+
or
33+
zeroDiff = false and sameLocAsCall = true
34+
)
35+
}
36+
1437
/** Gets the sub-expression of 'e' with the earliest-starting Location */
1538
Expr normalizeExpr(Expr e) {
16-
if forex(Expr q | q = e.(Call).getQualifier() | not q.(ThisExpr).isCompilerGenerated())
39+
if forex(Expr q | q = e.(Call).getQualifier() | not isCompilerGenerated(q.(ThisExpr)))
1740
then result = normalizeExpr(e.(Call).getQualifier())
1841
else
19-
if forex(Expr q | q = e.(FieldAccess).getQualifier() | not q.(ThisExpr).isCompilerGenerated())
42+
if forex(Expr q | q = e.(FieldAccess).getQualifier() | not isCompilerGenerated(q.(ThisExpr)))
2043
then result = normalizeExpr(e.(FieldAccess).getQualifier())
2144
else
2245
if e.hasExplicitConversion()

cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct X {
1616
#define BAZ //printf
1717

1818
struct Foo {
19-
int i;
19+
int i, i_array[3];
2020
int j;
2121
virtual int foo(int) = 0;
2222
virtual int bar(int, int) = 0;
@@ -27,9 +27,9 @@ struct Foo {
2727
void tutu() {}
2828
long toto() { return 42; }
2929
} titi;
30-
} *tata;
3130

32-
Tata::Titi **titi_ptr_ptr;
31+
Titi *operator->() { return &titi; }
32+
} *tata;
3333
};
3434

3535
int Foo::test(int (*baz)(int))
@@ -38,7 +38,7 @@ int Foo::test(int (*baz)(int))
3838

3939
if (i)
4040
(void)i, // GOOD
41-
(void)j;
41+
j++;
4242

4343
if (i)
4444
this->foo(i), // GOOD
@@ -181,8 +181,8 @@ int Foo::test(int (*baz)(int))
181181
(tata->titi.tutu(),
182182
foo(tata->titi.toto())); // GOOD
183183

184-
(*titi_ptr_ptr)->tutu(), // GOOD
185-
(&i)[0] += (int)(*titi_ptr_ptr)->toto();
184+
(*tata)->toto(), // GOOD
185+
i_array[i] += (int)(*tata)->toto();
186186

187187
return quux;
188188
}

0 commit comments

Comments
 (0)