Skip to content

Commit a6bb63f

Browse files
committed
Merge branch 'main' into rust-ti-implementing-type-method
2 parents 3f4bf76 + bab84d0 commit a6bb63f

File tree

41 files changed

+112
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+112
-147
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "a.h"
2+
#define FOUR 4
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main() {
2+
return ONE + FOUR;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#import "d.h"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main() {
2+
return SEVENTEEN;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if 1
2+
#pragma hdrstop
3+
extern int x;
4+
#define SEEN_F
5+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ifdef SEEN_F
2+
static int g() {
3+
return 20;
4+
}
5+
#endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "h1.h"
2+
#pragma hdrstop
3+
#include "h2.h"
4+
#define SEEN_H

cpp/ql/test/header-variant-tests/clang-pch/i.c renamed to cpp/ql/integration-tests/header-variant-tests/clang-pch/i.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ static int h2() {
1313
return 32;
1414
}
1515
#endif
16-
// semmle-extractor-options: --clang -include-pch ${testdir}/clang-pch.testproj/h.pch
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
4+
def test(codeql, cpp):
5+
os.mkdir("pch")
6+
extractor = cpp.get_tool("extractor")
7+
codeql.database.create(command=[
8+
f'"{extractor}" --mimic-clang -emit-pch -o pch/a.pch a.c',
9+
f'"{extractor}" --mimic-clang -include-pch pch/a.pch -Iextra_dummy_path b.c',
10+
f'"{extractor}" --mimic-clang -include pch/a -Iextra_dummy_path c.c',
11+
f'"{extractor}" --mimic-clang -emit-pch -o pch/d.pch d.c',
12+
f'"{extractor}" --mimic-clang -include-pch pch/d.pch e.c',
13+
f'"{extractor}" --mimic-clang -emit-pch -o pch/f.pch f.c',
14+
f'"{extractor}" --mimic-clang -include-pch pch/f.pch g.c',
15+
f'"{extractor}" --mimic-clang -emit-pch -o pch/h.pch h.c',
16+
f'"{extractor}" --mimic-clang -include-pch pch/h.pch i.c',
17+
])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "a.h"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma hdrstop
2+
#include "b.h"
3+
4+
int b() {
5+
return A;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "d.h"
2+
#include "c.h"
3+
4+
int c() {
5+
return A;
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
4+
def test(codeql, cpp):
5+
os.mkdir("pch")
6+
extractor = cpp.get_tool("extractor")
7+
codeql.database.create(command=[
8+
f'"{extractor}" --mimic-cl /Yca.h /Fppch/a.pch a.c',
9+
f'"{extractor}" --mimic-cl /Yub.h /Fppch/a.pch b.c',
10+
f'"{extractor}" --mimic-cl /Yuc.h /Fppch/a.pch c.c',
11+
])

cpp/ql/test/header-variant-tests/clang-pch/_.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/a.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/c.c

Lines changed: 0 additions & 4 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/d.c

Lines changed: 0 additions & 2 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/e.c

Lines changed: 0 additions & 4 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/f.c

Lines changed: 0 additions & 6 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/g.c

Lines changed: 0 additions & 6 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/clang-pch/h.c

Lines changed: 0 additions & 5 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/microsoft-pch/_.c

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/microsoft-pch/a.c

Lines changed: 0 additions & 2 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/microsoft-pch/b.c

Lines changed: 0 additions & 7 deletions
This file was deleted.

cpp/ql/test/header-variant-tests/microsoft-pch/c.c

Lines changed: 0 additions & 7 deletions
This file was deleted.

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import rust
1111
private import SsaImpl as SsaImpl
1212
private import codeql.rust.controlflow.internal.Scope as Scope
1313
private import codeql.rust.internal.PathResolution
14+
private import codeql.rust.internal.TypeInference as TypeInference
1415
private import codeql.rust.controlflow.ControlFlowGraph
1516
private import codeql.rust.controlflow.CfgNodes
1617
private import codeql.rust.dataflow.Ssa
@@ -321,23 +322,6 @@ predicate lambdaCallExpr(CallExprCfgNode call, LambdaCallKind kind, ExprCfgNode
321322
exists(kind)
322323
}
323324

324-
/** Holds if `mc` implicitly borrows its receiver. */
325-
private predicate implicitBorrow(MethodCallExpr mc) {
326-
// Determining whether an implicit borrow happens depends on the type of the
327-
// receiever as well as the target. As a heuristic we simply check if the
328-
// target takes `self` as a borrow and limit the approximation to cases where
329-
// the receiver is a simple variable.
330-
mc.getReceiver() instanceof VariableAccess and
331-
mc.getStaticTarget().getParamList().getSelfParam().isRef()
332-
}
333-
334-
/** Holds if `mc` implicitly dereferences its receiver. */
335-
private predicate implicitDeref(MethodCallExpr mc) {
336-
// Similarly to `implicitBorrow` this is an approximation.
337-
mc.getReceiver() instanceof VariableAccess and
338-
not mc.getStaticTarget().getParamList().getSelfParam().isRef()
339-
}
340-
341325
// Defines a set of aliases needed for the `RustDataFlow` module
342326
private module Aliases {
343327
class DataFlowCallableAlias = DataFlowCallable;
@@ -520,15 +504,15 @@ module RustDataFlow implements InputSig<Location> {
520504

521505
pragma[nomagic]
522506
private predicate implicitDerefToReceiver(Node node1, ReceiverNode node2, ReferenceContent c) {
507+
TypeInference::receiverHasImplicitDeref(node1.asExpr().getExpr()) and
523508
node1.asExpr() = node2.getReceiver() and
524-
implicitDeref(node2.getMethodCall().getMethodCallExpr()) and
525509
exists(c)
526510
}
527511

528512
pragma[nomagic]
529513
private predicate implicitBorrowToReceiver(Node node1, ReceiverNode node2, ReferenceContent c) {
514+
TypeInference::receiverHasImplicitBorrow(node1.asExpr().getExpr()) and
530515
node1.asExpr() = node2.getReceiver() and
531-
implicitBorrow(node2.getMethodCall().getMethodCallExpr()) and
532516
exists(c)
533517
}
534518

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -702,15 +702,6 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
702702
tAdj = t
703703
)
704704
}
705-
706-
pragma[nomagic]
707-
additional Type inferReceiverType(AstNode n) {
708-
exists(Access a, AccessPosition apos |
709-
result = inferType(n) and
710-
n = a.getNodeAt(apos) and
711-
apos.isSelf()
712-
)
713-
}
714705
}
715706

716707
private module CallExprBaseMatching = Matching<CallExprBaseMatchingInput>;
@@ -730,7 +721,7 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
730721
|
731722
if apos.isSelf()
732723
then
733-
exists(Type receiverType | receiverType = CallExprBaseMatchingInput::inferReceiverType(n) |
724+
exists(Type receiverType | receiverType = inferType(n) |
734725
if receiverType = TRefType()
735726
then
736727
path = path0 and
@@ -853,15 +844,6 @@ private module FieldExprMatchingInput implements MatchingInputSig {
853844
tAdj = t
854845
)
855846
}
856-
857-
pragma[nomagic]
858-
additional Type inferReceiverType(AstNode n) {
859-
exists(Access a, AccessPosition apos |
860-
result = inferType(n) and
861-
n = a.getNodeAt(apos) and
862-
apos.isSelf()
863-
)
864-
}
865847
}
866848

867849
private module FieldExprMatching = Matching<FieldExprMatchingInput>;
@@ -880,7 +862,7 @@ private Type inferFieldExprType(AstNode n, TypePath path) {
880862
|
881863
if apos.isSelf()
882864
then
883-
exists(Type receiverType | receiverType = FieldExprMatchingInput::inferReceiverType(n) |
865+
exists(Type receiverType | receiverType = inferType(n) |
884866
if receiverType = TRefType()
885867
then
886868
// adjust for implicit deref
@@ -1022,6 +1004,28 @@ private module Cached {
10221004
)
10231005
}
10241006

1007+
/** Holds if `receiver` is the receiver of a method call with an implicit dereference. */
1008+
cached
1009+
predicate receiverHasImplicitDeref(AstNode receiver) {
1010+
exists(CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos |
1011+
apos.isSelf() and
1012+
receiver = a.getNodeAt(apos) and
1013+
inferType(receiver) = TRefType() and
1014+
CallExprBaseMatching::inferAccessType(a, apos, TypePath::nil()) != TRefType()
1015+
)
1016+
}
1017+
1018+
/** Holds if `receiver` is the receiver of a method call with an implicit borrow. */
1019+
cached
1020+
predicate receiverHasImplicitBorrow(AstNode receiver) {
1021+
exists(CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos |
1022+
apos.isSelf() and
1023+
receiver = a.getNodeAt(apos) and
1024+
CallExprBaseMatching::inferAccessType(a, apos, TypePath::nil()) = TRefType() and
1025+
inferType(receiver) != TRefType()
1026+
)
1027+
}
1028+
10251029
pragma[inline]
10261030
private Type inferRootTypeDeref(AstNode n) {
10271031
exists(Type t |
@@ -1128,3 +1132,24 @@ import Cached
11281132
* Gets a type that `n` infers to, if any.
11291133
*/
11301134
Type inferType(AstNode n) { result = inferType(n, TypePath::nil()) }
1135+
1136+
/** Provides predicates for debugging the type inference implementation. */
1137+
private module Debug {
1138+
private Locatable getRelevantLocatable() {
1139+
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
1140+
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1141+
filepath.matches("%/tauri/src/app/plugin.rs") and
1142+
startline = 54
1143+
)
1144+
}
1145+
1146+
Type debugInferType(AstNode n, TypePath path) {
1147+
n = getRelevantLocatable() and
1148+
result = inferType(n, path)
1149+
}
1150+
1151+
Function debugResolveMethodCallExpr(MethodCallExpr mce) {
1152+
mce = getRelevantLocatable() and
1153+
result = resolveMethodCallExpr(mce)
1154+
}
1155+
}

rust/ql/test/library-tests/dataflow/local/DataFlowStep.expected

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,33 +1931,16 @@ readStep
19311931
| main.rs:221:9:221:23 | ...::Some(...) | Some | main.rs:221:22:221:22 | n |
19321932
| main.rs:230:9:230:15 | Some(...) | Some | main.rs:230:14:230:14 | n |
19331933
| main.rs:234:9:234:15 | Some(...) | Some | main.rs:234:14:234:14 | n |
1934-
| main.rs:241:10:241:11 | s1 | &ref | main.rs:241:10:241:11 | receiver for s1 |
1935-
| main.rs:246:10:246:11 | s1 | &ref | main.rs:246:10:246:11 | receiver for s1 |
1936-
| main.rs:249:10:249:11 | s2 | &ref | main.rs:249:10:249:11 | receiver for s2 |
1937-
| main.rs:254:10:254:11 | s1 | &ref | main.rs:254:10:254:11 | receiver for s1 |
1938-
| main.rs:257:10:257:11 | s2 | &ref | main.rs:257:10:257:11 | receiver for s2 |
19391934
| main.rs:263:14:263:15 | s1 | Ok | main.rs:263:14:263:16 | TryExpr |
19401935
| main.rs:263:14:263:15 | s1 | Some | main.rs:263:14:263:16 | TryExpr |
19411936
| main.rs:265:10:265:11 | s2 | Ok | main.rs:265:10:265:12 | TryExpr |
19421937
| main.rs:265:10:265:11 | s2 | Some | main.rs:265:10:265:12 | TryExpr |
1943-
| main.rs:271:29:271:30 | r1 | &ref | main.rs:271:29:271:30 | receiver for r1 |
1944-
| main.rs:272:29:272:30 | r1 | &ref | main.rs:272:29:272:30 | receiver for r1 |
1945-
| main.rs:273:10:273:12 | o1a | &ref | main.rs:273:10:273:12 | receiver for o1a |
1946-
| main.rs:274:10:274:12 | o1b | &ref | main.rs:274:10:274:12 | receiver for o1b |
1947-
| main.rs:277:29:277:30 | r2 | &ref | main.rs:277:29:277:30 | receiver for r2 |
1948-
| main.rs:278:29:278:30 | r2 | &ref | main.rs:278:29:278:30 | receiver for r2 |
1949-
| main.rs:279:10:279:12 | o2a | &ref | main.rs:279:10:279:12 | receiver for o2a |
1950-
| main.rs:280:10:280:12 | o2b | &ref | main.rs:280:10:280:12 | receiver for o2b |
19511938
| main.rs:287:14:287:15 | s1 | Ok | main.rs:287:14:287:16 | TryExpr |
19521939
| main.rs:287:14:287:15 | s1 | Some | main.rs:287:14:287:16 | TryExpr |
19531940
| main.rs:288:14:288:15 | s2 | Ok | main.rs:288:14:288:16 | TryExpr |
19541941
| main.rs:288:14:288:15 | s2 | Some | main.rs:288:14:288:16 | TryExpr |
19551942
| main.rs:291:14:291:15 | s3 | Ok | main.rs:291:14:291:16 | TryExpr |
19561943
| main.rs:291:14:291:15 | s3 | Some | main.rs:291:14:291:16 | TryExpr |
1957-
| main.rs:298:10:298:11 | s1 | &ref | main.rs:298:10:298:11 | receiver for s1 |
1958-
| main.rs:299:10:299:11 | s1 | &ref | main.rs:299:10:299:11 | receiver for s1 |
1959-
| main.rs:302:10:302:11 | s2 | &ref | main.rs:302:10:302:11 | receiver for s2 |
1960-
| main.rs:303:10:303:11 | s2 | &ref | main.rs:303:10:303:11 | receiver for s2 |
19611944
| main.rs:315:9:315:25 | ...::A(...) | A | main.rs:315:24:315:24 | n |
19621945
| main.rs:316:9:316:25 | ...::B(...) | B | main.rs:316:24:316:24 | n |
19631946
| main.rs:319:9:319:25 | ...::A(...) | A | main.rs:319:24:319:24 | n |
@@ -1997,40 +1980,22 @@ readStep
19971980
| main.rs:442:9:442:20 | TuplePat | tuple.0 | main.rs:442:10:442:13 | cond |
19981981
| main.rs:442:9:442:20 | TuplePat | tuple.1 | main.rs:442:16:442:19 | name |
19991982
| main.rs:442:25:442:29 | names | element | main.rs:442:9:442:20 | TuplePat |
2000-
| main.rs:444:21:444:24 | name | &ref | main.rs:444:21:444:24 | receiver for name |
20011983
| main.rs:444:41:444:67 | [post] \|...\| ... | captured default_name | main.rs:444:41:444:67 | [post] default_name |
2002-
| main.rs:444:44:444:55 | default_name | &ref | main.rs:444:44:444:55 | receiver for default_name |
20031984
| main.rs:444:44:444:55 | this | captured default_name | main.rs:444:44:444:55 | default_name |
2004-
| main.rs:445:18:445:18 | n | &ref | main.rs:445:18:445:18 | receiver for n |
2005-
| main.rs:468:13:468:13 | a | &ref | main.rs:468:13:468:13 | receiver for a |
2006-
| main.rs:469:13:469:13 | b | &ref | main.rs:469:13:469:13 | receiver for b |
2007-
| main.rs:470:19:470:19 | b | &ref | main.rs:470:19:470:19 | receiver for b |
20081985
| main.rs:481:10:481:11 | vs | element | main.rs:481:10:481:14 | vs[0] |
2009-
| main.rs:482:11:482:12 | vs | &ref | main.rs:482:11:482:12 | receiver for vs |
20101986
| main.rs:482:11:482:35 | ... .unwrap() | &ref | main.rs:482:10:482:35 | * ... |
2011-
| main.rs:483:11:483:12 | vs | &ref | main.rs:483:11:483:12 | receiver for vs |
20121987
| main.rs:483:11:483:35 | ... .unwrap() | &ref | main.rs:483:10:483:35 | * ... |
20131988
| main.rs:485:14:485:15 | vs | element | main.rs:485:9:485:9 | v |
20141989
| main.rs:488:9:488:10 | &... | &ref | main.rs:488:10:488:10 | v |
2015-
| main.rs:488:15:488:16 | vs | &ref | main.rs:488:15:488:16 | receiver for vs |
20161990
| main.rs:488:15:488:23 | vs.iter() | element | main.rs:488:9:488:10 | &... |
2017-
| main.rs:492:27:492:28 | vs | &ref | main.rs:492:27:492:28 | receiver for vs |
20181991
| main.rs:493:9:493:10 | &... | &ref | main.rs:493:10:493:10 | v |
20191992
| main.rs:493:15:493:17 | vs2 | element | main.rs:493:9:493:10 | &... |
2020-
| main.rs:497:5:497:6 | vs | &ref | main.rs:497:5:497:6 | receiver for vs |
20211993
| main.rs:497:29:497:29 | x | &ref | main.rs:497:28:497:29 | * ... |
2022-
| main.rs:498:5:498:6 | vs | &ref | main.rs:498:5:498:6 | receiver for vs |
20231994
| main.rs:498:34:498:34 | x | &ref | main.rs:498:33:498:34 | * ... |
2024-
| main.rs:500:14:500:15 | vs | &ref | main.rs:500:14:500:15 | receiver for vs |
20251995
| main.rs:500:14:500:27 | vs.into_iter() | element | main.rs:500:9:500:9 | v |
20261996
| main.rs:506:10:506:15 | vs_mut | element | main.rs:506:10:506:18 | vs_mut[0] |
2027-
| main.rs:507:11:507:16 | vs_mut | &ref | main.rs:507:11:507:16 | receiver for vs_mut |
20281997
| main.rs:507:11:507:39 | ... .unwrap() | &ref | main.rs:507:10:507:39 | * ... |
2029-
| main.rs:508:11:508:16 | vs_mut | &ref | main.rs:508:11:508:16 | receiver for vs_mut |
20301998
| main.rs:508:11:508:39 | ... .unwrap() | &ref | main.rs:508:10:508:39 | * ... |
20311999
| main.rs:510:9:510:14 | &mut ... | &ref | main.rs:510:14:510:14 | v |
2032-
| main.rs:510:19:510:24 | vs_mut | &ref | main.rs:510:19:510:24 | receiver for vs_mut |
20332000
| main.rs:510:19:510:35 | vs_mut.iter_mut() | element | main.rs:510:9:510:14 | &mut ... |
20342001
| main.rs:524:11:524:15 | c_ref | &ref | main.rs:524:10:524:15 | * ... |
2035-
| main.rs:531:10:531:10 | a | &ref | main.rs:531:10:531:10 | receiver for a |
2036-
| main.rs:537:10:537:10 | b | &ref | main.rs:537:10:537:10 | receiver for b |

0 commit comments

Comments
 (0)