From e9fdbfabea62da0504d6625cd5564f7799152275 Mon Sep 17 00:00:00 2001 From: Alex Denisov Date: Tue, 22 Aug 2023 17:36:40 +0200 Subject: [PATCH] Swift: extract nextCall from ForEachStmt --- .../extractor/translators/StmtTranslator.cpp | 1 + swift/ql/.generated.list | 6 ++-- .../codeql/swift/generated/ParentChild.qll | 12 ++++++-- swift/ql/lib/codeql/swift/generated/Raw.qll | 5 ++++ .../swift/generated/stmt/ForEachStmt.qll | 28 +++++++++++++++++++ swift/ql/lib/swift.dbscheme | 6 ++++ swift/schema.py | 1 + 7 files changed, 53 insertions(+), 6 deletions(-) diff --git a/swift/extractor/translators/StmtTranslator.cpp b/swift/extractor/translators/StmtTranslator.cpp index b7776a104b7..f600f59951a 100644 --- a/swift/extractor/translators/StmtTranslator.cpp +++ b/swift/extractor/translators/StmtTranslator.cpp @@ -76,6 +76,7 @@ codeql::ForEachStmt StmtTranslator::translateForEachStmt(const swift::ForEachStm entry.sequence = dispatcher.fetchLabel(stmt.getTypeCheckedSequence()); entry.pattern = dispatcher.fetchLabel(stmt.getPattern()); entry.where = dispatcher.fetchOptionalLabel(stmt.getWhere()); + entry.nextCall = dispatcher.fetchOptionalLabel(stmt.getNextCall()); return entry; } diff --git a/swift/ql/.generated.list b/swift/ql/.generated.list index f4a8f20d820..8b2b65b3cce 100644 --- a/swift/ql/.generated.list +++ b/swift/ql/.generated.list @@ -379,10 +379,10 @@ lib/codeql/swift/generated/KeyPathComponent.qll c79c7bc04fc1426992ab472eedc1a20a lib/codeql/swift/generated/Locatable.qll be20967d48a34cdba126fe298606e0adc11697831f097acba9c52a0b7ce9983e 8aa01bc376614abbc3209e25785c72f86c9b4e94bb5f471a4a0677fedaec4f61 lib/codeql/swift/generated/Location.qll c5793987e77812059a28254dadee29bfe9b38153c0399fbb1bf6a2f5c237fdab 6e6d8802b021e36bbaad81845657769dd48a798ea33080ada05e9818a20b38f7 lib/codeql/swift/generated/OtherAvailabilitySpec.qll 0e26a203b26ff0581b7396b0c6d1606feec5cc32477f676585cdec4911af91c5 0e26a203b26ff0581b7396b0c6d1606feec5cc32477f676585cdec4911af91c5 -lib/codeql/swift/generated/ParentChild.qll f8647fba02b9acca7bf2870dfaee5709e2d3e3a12d27b012dd1e17f7df2e56e5 75d3501c2a59d931dd537321475687a73ff517e5caaae4ce2e0c2daec0d94df4 +lib/codeql/swift/generated/ParentChild.qll 04b2871246a5d9c028de9add03077ee8728a418bd03f72fc2585fedd6b3ab630 03ccfa01fad473385a3fa467e0a784419955a1437fd5e432da3877b0ac369186 lib/codeql/swift/generated/PlatformVersionAvailabilitySpec.qll f82d9ca416fe8bd59b5531b65b1c74c9f317b3297a6101544a11339a1cffce38 7f5c6d3309e66c134107afe55bae76dfc9a72cb7cdd6d4c3706b6b34cee09fa0 lib/codeql/swift/generated/PureSynthConstructors.qll 173c0dd59396a1de26fe870e3bc2766c46de689da2a4d8807cb62023bbce1a98 173c0dd59396a1de26fe870e3bc2766c46de689da2a4d8807cb62023bbce1a98 -lib/codeql/swift/generated/Raw.qll 7904614a526f13c336402c38e8632c8ee32e0ee7a6b5a9c2ace22fab0a5927f8 273f958f5052ae025e7361dbfd6a7a505da5fa6b4f418e83aa2a1d5f8602c54d +lib/codeql/swift/generated/Raw.qll a216c5a0fd2331229db65795df7c2380ac51c7bc735c43ca985560183071d646 6afeb64476f461421c9e0958240e9771a543b198d78eb880325a436fd3a51f04 lib/codeql/swift/generated/Synth.qll 551fdf7e4b53f9ee1314d1bb42c2638cf82f45bfa1f40a635dfa7b6072e4418c 9ab178464700a19951fc5285acacda4913addee81515d8e072b3d7055935a814 lib/codeql/swift/generated/SynthConstructors.qll 2f801bd8b0db829b0253cd459ed3253c1fdfc55dce68ebc53e7fec138ef0aca4 2f801bd8b0db829b0253cd459ed3253c1fdfc55dce68ebc53e7fec138ef0aca4 lib/codeql/swift/generated/UnknownFile.qll 0fcf9beb8de79440bcdfff4bb6ab3dd139bd273e6c32754e05e6a632651e85f6 0fcf9beb8de79440bcdfff4bb6ab3dd139bd273e6c32754e05e6a632651e85f6 @@ -576,7 +576,7 @@ lib/codeql/swift/generated/stmt/DoCatchStmt.qll b418fdb6d48c2c0d30d11c0b256692af lib/codeql/swift/generated/stmt/DoStmt.qll 582f56113ecc384ee80610ae80e2a040fbe58c56b72c76b6c7da3daaeee739bd 3778445dc2f6173d4182cbda47ca0d0e066d931379ed7da89bb3afd1fda1e81b lib/codeql/swift/generated/stmt/FailStmt.qll d8f5816c51c5027fd6dacc8d9f5ddd21f691c138dfc80c6c79e250402a1fe165 d8f5816c51c5027fd6dacc8d9f5ddd21f691c138dfc80c6c79e250402a1fe165 lib/codeql/swift/generated/stmt/FallthroughStmt.qll aa400a95593395d97b196a78462fb5ab7cad0497b395cdd98885e1593271614d 4df6bfa7d2f4e2b5e5155351e445bb6c710e7c20c82fa3321564b11ef60b086a -lib/codeql/swift/generated/stmt/ForEachStmt.qll 0c4b3c9540aaf89c135de6618dc7f07680a44bb6e874d8b12b2457ecad7d766d 52fbec89382d3b207f379e126654008393be560c8efe4a490fda1e2c48914235 +lib/codeql/swift/generated/stmt/ForEachStmt.qll 766bdc3ceb4271c2813d4c6dbdd784b6d1d098094aa8caa439868493bad35648 b436469bbb584b35a02a143a3132dd728822e55cc03b041ffc5bb822a863d450 lib/codeql/swift/generated/stmt/GuardStmt.qll f31660bbe32231e310ff3d33dfece761ee7ec888fe74683359f86a3766e7c378 ce1f8279839e0b6311107ea9473871cbcfdc7c12d2368ac55b989f9bff2c5e7c lib/codeql/swift/generated/stmt/IfStmt.qll 80f1caba3a477e589b6aa3543ec1787005ab1ffab91a77832512c79dffce48c7 2126cf386e917a230175ba7e07450e390b4bd65da6fce1af8395e5ffd3f79dca lib/codeql/swift/generated/stmt/LabeledConditionalStmt.qll 057c6c556ecd836ca7f40d208c04e43039dde53e41eb27cc27f5f502a38a86fa 2ee979a35e0e9fa72253ab21d57c18b7268b7acc1edb4ec514b73b99b0aa2c6c diff --git a/swift/ql/lib/codeql/swift/generated/ParentChild.qll b/swift/ql/lib/codeql/swift/generated/ParentChild.qll index 8a91ccc7b82..2ae9729f1ce 100644 --- a/swift/ql/lib/codeql/swift/generated/ParentChild.qll +++ b/swift/ql/lib/codeql/swift/generated/ParentChild.qll @@ -3668,7 +3668,10 @@ private module Impl { private Element getImmediateChildOfForEachStmt( ForEachStmt e, int index, string partialPredicateCall ) { - exists(int b, int bLabeledStmt, int n, int nPattern, int nSequence, int nWhere, int nBody | + exists( + int b, int bLabeledStmt, int n, int nPattern, int nSequence, int nWhere, int nNextCall, + int nBody + | b = 0 and bLabeledStmt = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfLabeledStmt(e, i, _)) | i) and @@ -3676,7 +3679,8 @@ private module Impl { nPattern = n + 1 and nSequence = nPattern + 1 and nWhere = nSequence + 1 and - nBody = nWhere + 1 and + nNextCall = nWhere + 1 and + nBody = nNextCall + 1 and ( none() or @@ -3690,7 +3694,9 @@ private module Impl { or index = nSequence and result = e.getImmediateWhere() and partialPredicateCall = "Where()" or - index = nWhere and result = e.getBody() and partialPredicateCall = "Body()" + index = nWhere and result = e.getImmediateNextCall() and partialPredicateCall = "NextCall()" + or + index = nNextCall and result = e.getBody() and partialPredicateCall = "Body()" ) ) } diff --git a/swift/ql/lib/codeql/swift/generated/Raw.qll b/swift/ql/lib/codeql/swift/generated/Raw.qll index 122cb0291da..b7d3cd12bbc 100644 --- a/swift/ql/lib/codeql/swift/generated/Raw.qll +++ b/swift/ql/lib/codeql/swift/generated/Raw.qll @@ -2780,6 +2780,11 @@ module Raw { */ Expr getWhere() { for_each_stmt_wheres(this, result) } + /** + * Gets the nextcall of this for each statement, if it exists. + */ + Expr getNextCall() { for_each_stmt_next_calls(this, result) } + /** * Gets the body of this for each statement. */ diff --git a/swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll b/swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll index ad5422a7ec0..e0467f83926 100644 --- a/swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll +++ b/swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll @@ -82,6 +82,34 @@ module Generated { */ final predicate hasWhere() { exists(this.getWhere()) } + /** + * Gets the nextcall of this for each statement, if it exists. + * + * This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the + * behavior of both the `Immediate` and non-`Immediate` versions. + */ + Expr getImmediateNextCall() { + result = + Synth::convertExprFromRaw(Synth::convertForEachStmtToRaw(this) + .(Raw::ForEachStmt) + .getNextCall()) + } + + /** + * Gets the nextcall of this for each statement, if it exists. + */ + final Expr getNextCall() { + exists(Expr immediate | + immediate = this.getImmediateNextCall() and + result = immediate.resolve() + ) + } + + /** + * Holds if `getNextCall()` exists. + */ + final predicate hasNextCall() { exists(this.getNextCall()) } + /** * Gets the body of this for each statement. */ diff --git a/swift/ql/lib/swift.dbscheme b/swift/ql/lib/swift.dbscheme index 147e087e57e..c7087ed249c 100644 --- a/swift/ql/lib/swift.dbscheme +++ b/swift/ql/lib/swift.dbscheme @@ -1918,6 +1918,12 @@ for_each_stmt_wheres( //dir=stmt int where: @expr_or_none ref ); +#keyset[id] +for_each_stmt_next_calls( //dir=stmt + int id: @for_each_stmt ref, + int nextCall: @expr_or_none ref +); + @labeled_conditional_stmt = @guard_stmt | @if_stmt diff --git a/swift/schema.py b/swift/schema.py index 246cf5d0226..05cc4482bcd 100644 --- a/swift/schema.py +++ b/swift/schema.py @@ -988,6 +988,7 @@ class ForEachStmt(LabeledStmt): pattern: Pattern | child sequence: Expr | child where: optional[Expr] | child + nextCall: optional[Expr] | child body: BraceStmt | child class LabeledConditionalStmt(LabeledStmt):