Skip to content

Commit 6c10ba2

Browse files
authored
Merge pull request github#14495 from github/calumgrant/comp-generated-this
C++: Fix ImplicitThisFieldAccess
2 parents f562d53 + 8054a5d commit 6c10ba2

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* More field accesses are identified as `ImplicitThisFieldAccess`.

cpp/ql/lib/semmle/code/cpp/exprs/Access.qll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,13 @@ private predicate exprHasReferenceConversion(Expr e) { referenceConversion(e.get
306306
* }
307307
* };
308308
* ```
309-
* Note: the C++ front-end often automatically desugars `field` to
310-
* `this->field`, so most accesses of `this->field` are instances
311-
* of `PointerFieldAccess` (with `ThisExpr` as the qualifier), not
312-
* `ImplicitThisFieldAccess`.
313309
*/
314310
class ImplicitThisFieldAccess extends FieldAccess {
315311
override string getAPrimaryQlClass() { result = "ImplicitThisFieldAccess" }
316312

317-
ImplicitThisFieldAccess() { not exists(this.getQualifier()) }
313+
ImplicitThisFieldAccess() {
314+
this.getQualifier().(ThisExpr).isCompilerGenerated() or not exists(this.getQualifier())
315+
}
318316
}
319317

320318
/**
@@ -332,7 +330,7 @@ class PointerToFieldLiteral extends ImplicitThisFieldAccess {
332330
// access without a qualifier. The only other unqualified field accesses it
333331
// emits are for compiler-generated constructors and destructors. When we
334332
// filter those out, there are only pointer-to-field literals left.
335-
not this.isCompilerGenerated()
333+
not this.isCompilerGenerated() and not exists(this.getQualifier())
336334
}
337335

338336
override predicate isConstant() { any() }

cpp/ql/lib/semmle/code/cpp/valuenumbering/HashCons.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ private predicate analyzablePointerFieldAccess(PointerFieldAccess access) {
372372
private predicate mk_PointerFieldAccess(HashCons qualifier, Field target, PointerFieldAccess access) {
373373
analyzablePointerFieldAccess(access) and
374374
target = access.getTarget() and
375-
qualifier = hashCons(access.getQualifier().getFullyConverted())
375+
qualifier = hashCons(access.getQualifier().getFullyConverted()) and
376+
not access instanceof ImplicitThisFieldAccess
376377
}
377378

378379
private predicate analyzableImplicitThisFieldAccess(ImplicitThisFieldAccess access) {

cpp/ql/test/examples/expressions/PrintAST.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ union_etc.cpp:
12981298
# 6| getExpr(): [AssignExpr] ... = ...
12991299
# 6| Type = [IntType] int
13001300
# 6| ValueCategory = lvalue
1301-
# 6| getLValue(): [PointerFieldAccess] x
1301+
# 6| getLValue(): [ImplicitThisFieldAccess,PointerFieldAccess] x
13021302
# 6| Type = [IntType] int
13031303
# 6| ValueCategory = lvalue
13041304
# 6| getQualifier(): [ThisExpr] this
@@ -1488,7 +1488,7 @@ union_etc.cpp:
14881488
# 33| getExpr(): [AssignExpr] ... = ...
14891489
# 33| Type = [IntType] int
14901490
# 33| ValueCategory = lvalue
1491-
# 33| getLValue(): [PointerFieldAccess] q
1491+
# 33| getLValue(): [ImplicitThisFieldAccess,PointerFieldAccess] q
14921492
# 33| Type = [IntType] int
14931493
# 33| ValueCategory = lvalue
14941494
# 33| getQualifier(): [ThisExpr] this

cpp/ql/test/library-tests/access/FieldAccess/FieldAccess.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| FieldAccess.cpp:11:12:11:13 | p1 | ptr |
2-
| FieldAccess.cpp:12:12:12:13 | p2 | ptr |
3-
| FieldAccess.cpp:25:12:25:13 | x1 | ptr |
1+
| FieldAccess.cpp:11:12:11:13 | p1 | ptr, this |
2+
| FieldAccess.cpp:12:12:12:13 | p2 | ptr, this |
3+
| FieldAccess.cpp:25:12:25:13 | x1 | ptr, this |
44
| FieldAccess.cpp:29:18:29:19 | x2 | ptr |
55
| FieldAccess.cpp:34:3:34:3 | d | this |
66
| FieldAccess.cpp:45:13:45:14 | x1 | ptr |
@@ -19,10 +19,10 @@
1919
| FieldAccess.cpp:91:7:91:7 | x | val |
2020
| FieldAccess.cpp:91:13:91:13 | y | ref |
2121
| FieldAccess.cpp:92:8:92:8 | x | ptr |
22-
| FieldAccess.cpp:92:12:92:12 | y | ptr |
22+
| FieldAccess.cpp:92:12:92:12 | y | ptr, this |
2323
| FieldAccess.cpp:93:8:93:8 | x | ptr |
2424
| FieldAccess.cpp:93:18:93:18 | y | ptr |
2525
| FieldAccess.cpp:94:11:94:11 | y | ptr |
2626
| FieldAccess.cpp:94:20:94:20 | y | val |
27-
| FieldAccess.cpp:113:5:113:5 | x | ptr |
27+
| FieldAccess.cpp:113:5:113:5 | x | ptr, this |
2828
| FieldAccess.cpp:116:3:116:3 | v | this |

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ bad_asts.cpp:
8383
# 10| Type = [IntType] int
8484
# 10| Value = [Literal] 6
8585
# 10| ValueCategory = prvalue
86-
# 10| getRightOperand(): [PointerFieldAccess] x
86+
# 10| getRightOperand(): [ImplicitThisFieldAccess,PointerFieldAccess] x
8787
# 10| Type = [IntType] int
8888
# 10| ValueCategory = prvalue(load)
8989
# 10| getQualifier(): [ThisExpr] this
@@ -108,7 +108,7 @@ bad_asts.cpp:
108108
# 10| Type = [IntType] int
109109
# 10| Value = [Literal] t
110110
# 10| ValueCategory = prvalue
111-
# 10| getRightOperand(): [PointerFieldAccess] x
111+
# 10| getRightOperand(): [ImplicitThisFieldAccess,PointerFieldAccess] x
112112
# 10| Type = [IntType] int
113113
# 10| ValueCategory = prvalue(load)
114114
# 10| getQualifier(): [ThisExpr] this
@@ -5718,7 +5718,7 @@ ir.cpp:
57185718
# 645| getExpr(): [AssignExpr] ... = ...
57195719
# 645| Type = [IntType] int
57205720
# 645| ValueCategory = lvalue
5721-
# 645| getLValue(): [PointerFieldAccess] m_a
5721+
# 645| getLValue(): [ImplicitThisFieldAccess,PointerFieldAccess] m_a
57225722
# 645| Type = [IntType] int
57235723
# 645| ValueCategory = lvalue
57245724
# 645| getQualifier(): [ThisExpr] this
@@ -5770,7 +5770,7 @@ ir.cpp:
57705770
# 649| getLValue(): [VariableAccess] x
57715771
# 649| Type = [IntType] int
57725772
# 649| ValueCategory = lvalue
5773-
# 649| getRValue(): [PointerFieldAccess] m_a
5773+
# 649| getRValue(): [ImplicitThisFieldAccess,PointerFieldAccess] m_a
57745774
# 649| Type = [IntType] int
57755775
# 649| ValueCategory = prvalue(load)
57765776
# 649| getQualifier(): [ThisExpr] this
@@ -9018,7 +9018,7 @@ ir.cpp:
90189018
# 1043| getArrayBase(): [FunctionCall] call to c_str
90199019
# 1043| Type = [PointerType] const char *
90209020
# 1043| ValueCategory = prvalue
9021-
# 1043| getQualifier(): [PointerFieldAccess] s
9021+
# 1043| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
90229022
# 1043| Type = [LValueReferenceType] const String &
90239023
# 1043| ValueCategory = prvalue(load)
90249024
# 1043| getQualifier(): [ThisExpr] this
@@ -9027,7 +9027,7 @@ ir.cpp:
90279027
# 1043| getQualifier().getFullyConverted(): [ReferenceDereferenceExpr] (reference dereference)
90289028
# 1043| Type = [SpecifiedType] const String
90299029
# 1043| ValueCategory = lvalue
9030-
# 1043| getArrayOffset(): [PointerFieldAccess] x
9030+
# 1043| getArrayOffset(): [ImplicitThisFieldAccess,PointerFieldAccess] x
90319031
# 1043| Type = [LValueReferenceType] int &
90329032
# 1043| ValueCategory = prvalue(load)
90339033
# 1043| getQualifier(): [ThisExpr] this
@@ -9076,13 +9076,13 @@ ir.cpp:
90769076
# 1045| getArrayBase(): [FunctionCall] call to c_str
90779077
# 1045| Type = [PointerType] const char *
90789078
# 1045| ValueCategory = prvalue
9079-
# 1045| getQualifier(): [PointerFieldAccess] s
9079+
# 1045| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
90809080
# 1045| Type = [SpecifiedType] const String
90819081
# 1045| ValueCategory = lvalue
90829082
# 1045| getQualifier(): [ThisExpr] this
90839083
# 1045| Type = [PointerType] const lambda [] type at line 1045, col. 21 *
90849084
# 1045| ValueCategory = prvalue(load)
9085-
# 1045| getArrayOffset(): [PointerFieldAccess] x
9085+
# 1045| getArrayOffset(): [ImplicitThisFieldAccess,PointerFieldAccess] x
90869086
# 1045| Type = [IntType] int
90879087
# 1045| ValueCategory = prvalue(load)
90889088
# 1045| getQualifier(): [ThisExpr] this
@@ -9114,7 +9114,7 @@ ir.cpp:
91149114
# 1047| getArrayBase(): [FunctionCall] call to c_str
91159115
# 1047| Type = [PointerType] const char *
91169116
# 1047| ValueCategory = prvalue
9117-
# 1047| getQualifier(): [PointerFieldAccess] s
9117+
# 1047| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
91189118
# 1047| Type = [LValueReferenceType] const String &
91199119
# 1047| ValueCategory = prvalue(load)
91209120
# 1047| getQualifier(): [ThisExpr] this
@@ -9167,7 +9167,7 @@ ir.cpp:
91679167
# 1049| getArrayBase(): [FunctionCall] call to c_str
91689168
# 1049| Type = [PointerType] const char *
91699169
# 1049| ValueCategory = prvalue
9170-
# 1049| getQualifier(): [PointerFieldAccess] s
9170+
# 1049| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
91719171
# 1049| Type = [SpecifiedType] const String
91729172
# 1049| ValueCategory = lvalue
91739173
# 1049| getQualifier(): [ThisExpr] this
@@ -9203,7 +9203,7 @@ ir.cpp:
92039203
# 1051| getArrayBase(): [FunctionCall] call to c_str
92049204
# 1051| Type = [PointerType] const char *
92059205
# 1051| ValueCategory = prvalue
9206-
# 1051| getQualifier(): [PointerFieldAccess] s
9206+
# 1051| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
92079207
# 1051| Type = [LValueReferenceType] const String &
92089208
# 1051| ValueCategory = prvalue(load)
92099209
# 1051| getQualifier(): [ThisExpr] this
@@ -9212,7 +9212,7 @@ ir.cpp:
92129212
# 1051| getQualifier().getFullyConverted(): [ReferenceDereferenceExpr] (reference dereference)
92139213
# 1051| Type = [SpecifiedType] const String
92149214
# 1051| ValueCategory = lvalue
9215-
# 1051| getArrayOffset(): [PointerFieldAccess] x
9215+
# 1051| getArrayOffset(): [ImplicitThisFieldAccess,PointerFieldAccess] x
92169216
# 1051| Type = [IntType] int
92179217
# 1051| ValueCategory = prvalue(load)
92189218
# 1051| getQualifier(): [ThisExpr] this
@@ -9244,7 +9244,7 @@ ir.cpp:
92449244
# 1054| getArrayBase(): [FunctionCall] call to c_str
92459245
# 1054| Type = [PointerType] const char *
92469246
# 1054| ValueCategory = prvalue
9247-
# 1054| getQualifier(): [PointerFieldAccess] s
9247+
# 1054| getQualifier(): [ImplicitThisFieldAccess,PointerFieldAccess] s
92489248
# 1054| Type = [LValueReferenceType] const String &
92499249
# 1054| ValueCategory = prvalue(load)
92509250
# 1054| getQualifier(): [ThisExpr] this
@@ -9259,7 +9259,7 @@ ir.cpp:
92599259
# 1054| getLeftOperand(): [AddExpr] ... + ...
92609260
# 1054| Type = [IntType] int
92619261
# 1054| ValueCategory = prvalue
9262-
# 1054| getLeftOperand(): [PointerFieldAccess] x
9262+
# 1054| getLeftOperand(): [ImplicitThisFieldAccess,PointerFieldAccess] x
92639263
# 1054| Type = [IntType] int
92649264
# 1054| ValueCategory = prvalue(load)
92659265
# 1054| getQualifier(): [ThisExpr] this
@@ -11490,7 +11490,7 @@ ir.cpp:
1149011490
# 1458| getExpr(): [AssignExpr] ... = ...
1149111491
# 1458| Type = [IntType] int
1149211492
# 1458| ValueCategory = lvalue
11493-
# 1458| getLValue(): [PointerFieldAccess] y
11493+
# 1458| getLValue(): [ImplicitThisFieldAccess,PointerFieldAccess] y
1149411494
# 1458| Type = [IntType] int
1149511495
# 1458| ValueCategory = lvalue
1149611496
# 1458| getQualifier(): [ThisExpr] this
@@ -12302,7 +12302,7 @@ ir.cpp:
1230212302
# 1567| <params>:
1230312303
# 1567| getEntryPoint(): [BlockStmt] { ... }
1230412304
# 1568| getStmt(0): [ReturnStmt] return ...
12305-
# 1568| getExpr(): [PointerFieldAccess] i
12305+
# 1568| getExpr(): [ImplicitThisFieldAccess,PointerFieldAccess] i
1230612306
# 1568| Type = [IntType] int
1230712307
# 1568| ValueCategory = lvalue
1230812308
# 1568| getQualifier(): [ThisExpr] this
@@ -12315,7 +12315,7 @@ ir.cpp:
1231512315
# 1571| <params>:
1231612316
# 1571| getEntryPoint(): [BlockStmt] { ... }
1231712317
# 1572| getStmt(0): [ReturnStmt] return ...
12318-
# 1572| getExpr(): [PointerFieldAccess] d
12318+
# 1572| getExpr(): [ImplicitThisFieldAccess,PointerFieldAccess] d
1231912319
# 1572| Type = [DoubleType] double
1232012320
# 1572| ValueCategory = lvalue
1232112321
# 1572| getQualifier(): [ThisExpr] this
@@ -12328,7 +12328,7 @@ ir.cpp:
1232812328
# 1575| <params>:
1232912329
# 1575| getEntryPoint(): [BlockStmt] { ... }
1233012330
# 1576| getStmt(0): [ReturnStmt] return ...
12331-
# 1576| getExpr(): [PointerFieldAccess] r
12331+
# 1576| getExpr(): [ImplicitThisFieldAccess,PointerFieldAccess] r
1233212332
# 1576| Type = [LValueReferenceType] int &
1233312333
# 1576| ValueCategory = prvalue(load)
1233412334
# 1576| getQualifier(): [ThisExpr] this
@@ -12669,7 +12669,7 @@ ir.cpp:
1266912669
# 1633| <params>:
1267012670
# 1633| getEntryPoint(): [BlockStmt] { ... }
1267112671
# 1634| getStmt(0): [ReturnStmt] return ...
12672-
# 1634| getExpr(): [PointerFieldAccess] i
12672+
# 1634| getExpr(): [ImplicitThisFieldAccess,PointerFieldAccess] i
1267312673
# 1634| Type = [IntType] int
1267412674
# 1634| ValueCategory = prvalue(load)
1267512675
# 1634| getQualifier(): [ThisExpr] this
@@ -12679,7 +12679,7 @@ ir.cpp:
1267912679
# 1637| <params>:
1268012680
# 1637| getEntryPoint(): [BlockStmt] { ... }
1268112681
# 1638| getStmt(0): [ReturnStmt] return ...
12682-
# 1638| getExpr(): [PointerFieldAccess] r
12682+
# 1638| getExpr(): [ImplicitThisFieldAccess,PointerFieldAccess] r
1268312683
# 1638| Type = [LValueReferenceType] int &
1268412684
# 1638| ValueCategory = prvalue(load)
1268512685
# 1638| getQualifier(): [ThisExpr] this
@@ -13290,7 +13290,7 @@ ir.cpp:
1329013290
# 1703| getQualifier(): [AddressOfExpr] & ...
1329113291
# 1703| Type = [PointerType] const TrivialLambdaClass *
1329213292
# 1703| ValueCategory = prvalue
13293-
# 1703| getOperand(): [PointerFieldAccess] (captured this)
13293+
# 1703| getOperand(): [ImplicitThisFieldAccess,PointerFieldAccess] (captured this)
1329413294
# 1703| Type = [SpecifiedType] const TrivialLambdaClass
1329513295
# 1703| ValueCategory = lvalue
1329613296
# 1703| getQualifier(): [ThisExpr] this
@@ -13337,7 +13337,7 @@ ir.cpp:
1333713337
# 1706| getQualifier(): [AddressOfExpr] & ...
1333813338
# 1706| Type = [PointerType] const TrivialLambdaClass *
1333913339
# 1706| ValueCategory = prvalue
13340-
# 1706| getOperand(): [PointerFieldAccess] (captured this)
13340+
# 1706| getOperand(): [ImplicitThisFieldAccess,PointerFieldAccess] (captured this)
1334113341
# 1706| Type = [SpecifiedType] const TrivialLambdaClass
1334213342
# 1706| ValueCategory = lvalue
1334313343
# 1706| getQualifier(): [ThisExpr] this
@@ -13480,7 +13480,7 @@ ir.cpp:
1348013480
# 1726| getExpr(): [AssignExpr] ... = ...
1348113481
# 1726| Type = [IntType] int
1348213482
# 1726| ValueCategory = lvalue
13483-
# 1726| getLValue(): [PointerFieldAccess] x
13483+
# 1726| getLValue(): [ImplicitThisFieldAccess,PointerFieldAccess] x
1348413484
# 1726| Type = [IntType] int
1348513485
# 1726| ValueCategory = lvalue
1348613486
# 1726| getQualifier(): [ThisExpr] this

0 commit comments

Comments
 (0)