Swift: extract DiscardStmt

This commit is contained in:
Alex Denisov
2023-11-15 16:26:48 +01:00
parent 5cd74213cc
commit 6a673e700b
19 changed files with 212 additions and 11 deletions

View File

@@ -61,9 +61,7 @@ MAP(swift::Stmt, StmtTag)
MAP(swift::FailStmt, FailStmtTag)
MAP(swift::ThrowStmt, ThrowStmtTag)
MAP(swift::PoundAssertStmt, PoundAssertStmtTag)
#if CODEQL_SWIFT_VERSION_GE(5, 9)
MAP(swift::DiscardStmt, void) // TODO (introduced in 5.9)
#endif
MAP(swift::DiscardStmt, DiscardStmtTag)
MAP(swift::Argument, ArgumentTag)
MAP(swift::KeyPathExpr::Component, KeyPathComponentTag)

View File

@@ -212,4 +212,10 @@ codeql::PoundAssertStmt StmtTranslator::translatePoundAssertStmt(
return entry;
}
codeql::DiscardStmt StmtTranslator::translateDiscardStmt(const swift::DiscardStmt& stmt) {
auto entry = dispatcher.createEntry(stmt);
entry.sub_expr = dispatcher.fetchLabel(stmt.getSubExpr());
return entry;
}
} // namespace codeql

View File

@@ -39,6 +39,7 @@ class StmtTranslator : public AstTranslatorBase<StmtTranslator> {
codeql::YieldStmt translateYieldStmt(const swift::YieldStmt& stmt);
codeql::FailStmt translateFailStmt(const swift::FailStmt& stmt);
codeql::PoundAssertStmt translatePoundAssertStmt(const swift::PoundAssertStmt& stmt);
codeql::DiscardStmt translateDiscardStmt(const swift::DiscardStmt& stmt);
private:
void fillLabeledStmt(const swift::LabeledStmt& stmt, codeql::LabeledStmt& entry);

View File

@@ -255,6 +255,8 @@ lib/codeql/swift/elements/stmt/CaseStmtConstructor.qll a15229190cf8f081c14879bd1
lib/codeql/swift/elements/stmt/ConditionElementConstructor.qll 1eb1d95d5ed432fefebc8b7b8155676486d329ae2a287475ece687d8b1f22933 fd7b5e5e26b478680a1550138b4e857774091cfd07e73bb765bb58c167d03212
lib/codeql/swift/elements/stmt/ContinueStmtConstructor.qll c6f135ad0f77dc4ad63074ccc36bfff7c4ebc2d9b8c11e07fd20649a0cbaabfc c214e701b76e7d7362e2e7da1bddb6b59a9cb0e54106b0923b3694c358fdb3eb
lib/codeql/swift/elements/stmt/DeferStmtConstructor.qll dba11476a09e53f1f8bd9b1688f94dced836cef43543622b3880592e8fb18cee 192ce27dd2cd4b147fcba99d7741c816fa8cd9a1bf912e7588bcf32f0c5e6a7a
lib/codeql/swift/elements/stmt/DiscardStmt.qll 10429ddbf4a89e089c13b697ef88e0231e1a0e1783f875cf6e4d30081e6add4e 88ba363258ba9f6306bbb562604423b1f395608038c1b7fd50873d0b39314304
lib/codeql/swift/elements/stmt/DiscardStmtConstructor.qll 932b5998727c85d54a2b66c38c43f27de5961ba5d7a977f6b8b79d60aa190638 e3714d7387c2b6184f252a15d9e5214c1f24be20a7f520ba6321f85ae180283d
lib/codeql/swift/elements/stmt/DoCatchStmtConstructor.qll c2c0c70421bbab121b62c636c55cf11cf549a79be88a657190045c6d934238fb 2a8dcfabe50207c5ae62a958c3b246d4a532d812033934e4d6b33a80c99b4808
lib/codeql/swift/elements/stmt/DoStmtConstructor.qll 17190698940ae09221316c162b091865e61b314aa1b269c0ecca5b8f7a71a61d def0e73f98e5c756fcacc7ffea06e1a35c0da50729a726e3629ce78604bbaf39
lib/codeql/swift/elements/stmt/FailStmtConstructor.qll eeb8af9a8638166021be1b4c7b9ba99d00b2675e5af7104c303f8ae2ace81dc9 f6ad97c404fe37adb0dcc02b3c65f3dc8af268109af71a62f4aa74b923871aea
@@ -388,7 +390,7 @@ lib/codeql/swift/elements/type/UnresolvedTypeConstructor.qll 7f75d489b4d7ce65cae
lib/codeql/swift/elements/type/VariadicSequenceTypeConstructor.qll fc74a5a2a2effa28ef24509b20ee4373d97cf6e8c71840121bb031c6adedf584 c9b2effc1d01c13c5e6a74a111122fa79a2f6554dda3cb016d68ba397e566ec4
lib/codeql/swift/elements/type/WeakStorageType.qll edd13dd97b53040684409e187c1f975bcada6807c919e1345d8977144dbebb6f 9434c044d264a7f5f503a6422c106c9b8fedf74aaae314174473a29ea6ed17b9
lib/codeql/swift/elements/type/WeakStorageTypeConstructor.qll 5fdce3716aba6318522174a2c455a63480970222ae81c732fb19c6dd3ae2d271 60ea79d6943e129deba0deccb566cf9d73f78398b0f7f0212674d91287d6b2ae
lib/codeql/swift/elements.qll 16d27aa2184e864cca9da089068935b393c1ab08f2ad33a64ebe3f7f3c223636 16d27aa2184e864cca9da089068935b393c1ab08f2ad33a64ebe3f7f3c223636
lib/codeql/swift/elements.qll 27da89b0b50ea17b96a47aad31453ff0d43b8277ab660385f785879ce6cc243e 27da89b0b50ea17b96a47aad31453ff0d43b8277ab660385f785879ce6cc243e
lib/codeql/swift/generated/AstNode.qll 68877daa9e14b462247ac6b7b724f5e683288e39953a8ebb02a362b7d1df8e4c 54d3512744738e1ee15645f3af116437053cb5209687f4106361a1943b38b666
lib/codeql/swift/generated/AvailabilityInfo.qll e74e218a1ab00416cb8823610ff93642101aa784aa61cbc2b4deef61471a5bac e2c6c19860dc3e6e211041c95d8e6d52c3505ccff7018b80a849735cc98141af
lib/codeql/swift/generated/AvailabilitySpec.qll a8afc5071887a67b4e0dec27356ab8cbf3e176b5358cb34c785e3015b2cad5a2 c7f88b0d701612c821359c983b3102f31b23edc211c3dcfe97de5adec61af386
@@ -405,12 +407,12 @@ lib/codeql/swift/generated/Locatable.qll 6cb437dd7ff7331429ec6586b0af50b1af15e4f
lib/codeql/swift/generated/Location.qll 3f3bad413be87d05a596fe7b8004f415c2caa98cb759021a6aad20b589b7d700 ed30ed646962b3ffb6b47c97c6434fe47a6b1ea8e3f2e0589577bea5cf96c88e
lib/codeql/swift/generated/MacroRole.qll aaf5631c49de81e046854955341202d6d3516713cd09bc2e7b870e40c261cc9f 6cd17d40cbf1d8fa4ef01dfb8b3462b7cee902e6058fb76417c2035be12481d1
lib/codeql/swift/generated/OtherAvailabilitySpec.qll 06393a08e8da36106c5ec6efb9f1bd56a5c7b3d3f3d0bcefc6fa07fa96860c31 06393a08e8da36106c5ec6efb9f1bd56a5c7b3d3f3d0bcefc6fa07fa96860c31
lib/codeql/swift/generated/ParentChild.qll d0702695f17a129d51546ca7a4f8d2cc53e5b2ce1525a48ee1182b852f1935a5 c37d5ec2c208797db3698fa676cadce5f825a86ff38c0f803fcf500b0bfb57bb
lib/codeql/swift/generated/ParentChild.qll 281ac5b565c0d8b2c9cfc1a524024e3a2514034a5266b7a7d75ab3770fc398d3 2829f5e61adbd863f4ad823ecfd7c1bb5eccaf14bb121b85ad460175b733fe30
lib/codeql/swift/generated/PlatformVersionAvailabilitySpec.qll 5355be9da8b778d1d8ae60d25d9c3394477da24f94e8a6ab4484c6a16d07cd7c 075438c1762ec0a7775004b39032dcf85aada038a4269e6f428c34b8282786e9
lib/codeql/swift/generated/PureSynthConstructors.qll 40f5c0c573ce12f16322d9efb12306750f672254cbc36a200c298cb08e504229 40f5c0c573ce12f16322d9efb12306750f672254cbc36a200c298cb08e504229
lib/codeql/swift/generated/Raw.qll 18d26216cf811999fb3fd0025af22fbbbcb49047eb9a4f470e75a04d955ee962 8b80c9395b95efaa0c66a9e81f78570749c3e429b0d867af45a49be2f0b19823
lib/codeql/swift/generated/Synth.qll 7a34f9ad85123dabf2d3be871d4936287b8683f11169731ca43c5cefa358f388 690ac3af3e3e149504313aaa34a6f21bf36860733c3b468f53a893fa0f95633a
lib/codeql/swift/generated/SynthConstructors.qll 5959cd4a3fc672015ee9956e63cded1dce7093308c2ad25bd090f0e0c690ed11 5959cd4a3fc672015ee9956e63cded1dce7093308c2ad25bd090f0e0c690ed11
lib/codeql/swift/generated/Raw.qll 1915a5a46c0ba57ebc8a0e41c4e49e3f3d9ee21cd863d1c9a8a0a455e5e9e24a 04f4a7eb8deb7f501e4a98563283094e1d53f8f0f8c82809ed05a5e99dff6787
lib/codeql/swift/generated/Synth.qll b8bf274c60f60df473ed9093b50906822613dee047bda19ad37d07c308f04564 692590b0b18556a23cc1de0c8a60fd17534791dccb876cab85170bbf78392bd1
lib/codeql/swift/generated/SynthConstructors.qll d3b4b5d93be989004d7c05bbc32a5b859eaad768b4a52cfb01a767c90542f9a4 d3b4b5d93be989004d7c05bbc32a5b859eaad768b4a52cfb01a767c90542f9a4
lib/codeql/swift/generated/UnknownFile.qll 5325944cf96a72d5d224597745e15960fb6a9448b96b6644ececd6344dfd9d74 5325944cf96a72d5d224597745e15960fb6a9448b96b6644ececd6344dfd9d74
lib/codeql/swift/generated/UnknownLocation.qll dfdeb8eedb2564eccaac416695784ea04fe9754a3e109e8484c695021af4e554 dfdeb8eedb2564eccaac416695784ea04fe9754a3e109e8484c695021af4e554
lib/codeql/swift/generated/UnspecifiedElement.qll 8ecc275cc131fe5aa61052299e10c49c3718f96416df9eeacabf5aea34d97982 b02dfcf0df3859551b176e065291da943670ab4da6ed84d02a0861ff689001c6
@@ -606,6 +608,7 @@ lib/codeql/swift/generated/stmt/CaseStmt.qll f47e7f2ac015c900b57c87dc00e767f9609
lib/codeql/swift/generated/stmt/ConditionElement.qll 63d4f40e296ee0fe0b9d11cad297c1ba100d7edbf85b807701d28d611b1780a7 94b88f5e3130989749bf39186f74ed6c518ab575fbbc0ef198493a0774f02faf
lib/codeql/swift/generated/stmt/ContinueStmt.qll 8cf27c6e2053f4eec274978b8ddac65de8ace578f7d8f7a4db51dceb600a1c92 795b5c96323539e03bbf0b948ac9d1eaa0a418ba8c63475ece3eb8dedccdbfe9
lib/codeql/swift/generated/stmt/DeferStmt.qll 8f0e00ed08fd2c88f4b7caa25478783162bfce5c21538c972d2907244755f574 a2f8bd8dbe6d8ba7e972a62e2f508dc0ad0495bbf90ee4800f3337fc9597abef
lib/codeql/swift/generated/stmt/DiscardStmt.qll 5bb8b8f4256600b687c637f452747082b4d11f2a1b25801c19f2ed114980aea4 73f7feb221be79b803f1dc94f14b0f3e8dbc6be4fe4f6a9b4a3a4ad097c3da39
lib/codeql/swift/generated/stmt/DoCatchStmt.qll 64a1b2a612adc743ce0527b474dd3c81e5ebd02bf27b1289f127b2809fd807a8 629ada84f4f75785d295098fc9e4feaee629efe76de56f68f0e40f32bf790604
lib/codeql/swift/generated/stmt/DoStmt.qll 221f1da35e7530bfac6ea171a5c7ac89f65ec30ee6842580c422fd14e75cb4a3 cd0214af648dc87abebd2602016dfc9a9c388a7bb2bf78698f196e884d823db0
lib/codeql/swift/generated/stmt/FailStmt.qll ff9a2ae0f0bcb7b97504d9baa9f01d7912abeb017f118f32e98bb38460872639 ff9a2ae0f0bcb7b97504d9baa9f01d7912abeb017f118f32e98bb38460872639
@@ -920,6 +923,7 @@ test/extractor-tests/generated/stmt/BreakStmt/MISSING_SOURCE.txt 66846d526b0bc43
test/extractor-tests/generated/stmt/ConditionElement/MISSING_SOURCE.txt 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7
test/extractor-tests/generated/stmt/ContinueStmt/MISSING_SOURCE.txt 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7
test/extractor-tests/generated/stmt/DeferStmt/MISSING_SOURCE.txt 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7
test/extractor-tests/generated/stmt/DiscardStmt/DiscardStmt.ql 58aa6865388fb78d53fa67dc9c8afbe8dc20c47095c987799301a13b6c591a7a f352f731f16137b84f0617c3ebf374767ea679b4bae13e607a9354534c93aad8
test/extractor-tests/generated/stmt/DoCatchStmt/MISSING_SOURCE.txt 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7
test/extractor-tests/generated/stmt/DoStmt/MISSING_SOURCE.txt 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7 66846d526b0bc4328735c3c4dd9c390a9325da5b5dfd42ec07622f9c7108a7d7
test/extractor-tests/generated/stmt/FailStmt/FailStmt.ql 75bf8a697d3a610caf25cb0d25748f2d1620c20fdd84c278c3e2f2502bc3f418 6e2377b8d2a63deaadbf8661dc7da70e8523637020e7d5fd601ca6893f10a573

4
swift/ql/.gitattributes generated vendored
View File

@@ -257,6 +257,8 @@
/lib/codeql/swift/elements/stmt/ConditionElementConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/ContinueStmtConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/DeferStmtConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/DiscardStmt.qll linguist-generated
/lib/codeql/swift/elements/stmt/DiscardStmtConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/DoCatchStmtConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/DoStmtConstructor.qll linguist-generated
/lib/codeql/swift/elements/stmt/FailStmtConstructor.qll linguist-generated
@@ -608,6 +610,7 @@
/lib/codeql/swift/generated/stmt/ConditionElement.qll linguist-generated
/lib/codeql/swift/generated/stmt/ContinueStmt.qll linguist-generated
/lib/codeql/swift/generated/stmt/DeferStmt.qll linguist-generated
/lib/codeql/swift/generated/stmt/DiscardStmt.qll linguist-generated
/lib/codeql/swift/generated/stmt/DoCatchStmt.qll linguist-generated
/lib/codeql/swift/generated/stmt/DoStmt.qll linguist-generated
/lib/codeql/swift/generated/stmt/FailStmt.qll linguist-generated
@@ -922,6 +925,7 @@
/test/extractor-tests/generated/stmt/ConditionElement/MISSING_SOURCE.txt linguist-generated
/test/extractor-tests/generated/stmt/ContinueStmt/MISSING_SOURCE.txt linguist-generated
/test/extractor-tests/generated/stmt/DeferStmt/MISSING_SOURCE.txt linguist-generated
/test/extractor-tests/generated/stmt/DiscardStmt/DiscardStmt.ql linguist-generated
/test/extractor-tests/generated/stmt/DoCatchStmt/MISSING_SOURCE.txt linguist-generated
/test/extractor-tests/generated/stmt/DoStmt/MISSING_SOURCE.txt linguist-generated
/test/extractor-tests/generated/stmt/FailStmt/FailStmt.ql linguist-generated

View File

@@ -237,6 +237,14 @@ module Stmts {
}
}
private class DiscardStmtTree extends AstStandardPostOrderTree {
override DiscardStmt ast;
final override ControlFlowElement getChildElement(int i) {
i = 0 and result.asAstNode() = ast.getSubExpr().getFullyUnresolved()
}
}
private class YieldStmtTree extends AstStandardPostOrderTree {
override YieldStmt ast;

View File

@@ -211,6 +211,7 @@ import codeql.swift.elements.stmt.CaseStmt
import codeql.swift.elements.stmt.ConditionElement
import codeql.swift.elements.stmt.ContinueStmt
import codeql.swift.elements.stmt.DeferStmt
import codeql.swift.elements.stmt.DiscardStmt
import codeql.swift.elements.stmt.DoCatchStmt
import codeql.swift.elements.stmt.DoStmt
import codeql.swift.elements.stmt.FailStmt

View File

@@ -0,0 +1,16 @@
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
/**
* This module provides a hand-modifiable wrapper around the generated class `DiscardStmt`.
*/
private import codeql.swift.generated.stmt.DiscardStmt
/**
* A statement that takes a non-copyable value and destructs its members/fields.
*
* The only valid syntax:
* ```
* destruct self
* ```
*/
class DiscardStmt extends Generated::DiscardStmt { }

View File

@@ -0,0 +1,14 @@
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
/**
* This module defines the hook used internally to tweak the characteristic predicate of
* `DiscardStmt` synthesized instances.
* INTERNAL: Do not use.
*/
private import codeql.swift.generated.Raw
/**
* The characteristic predicate of `DiscardStmt` synthesized instances.
* INTERNAL: Do not use.
*/
predicate constructDiscardStmt(Raw::DiscardStmt id) { any() }

View File

@@ -3688,6 +3688,24 @@ private module Impl {
)
}
private Element getImmediateChildOfDiscardStmt(
DiscardStmt e, int index, string partialPredicateCall
) {
exists(int b, int bStmt, int n, int nSubExpr |
b = 0 and
bStmt = b + 1 + max(int i | i = -1 or exists(getImmediateChildOfStmt(e, i, _)) | i) and
n = bStmt and
nSubExpr = n + 1 and
(
none()
or
result = getImmediateChildOfStmt(e, index - b, partialPredicateCall)
or
index = n and result = e.getImmediateSubExpr() and partialPredicateCall = "SubExpr()"
)
)
}
private Element getImmediateChildOfFailStmt(FailStmt e, int index, string partialPredicateCall) {
exists(int b, int bStmt, int n |
b = 0 and
@@ -5438,6 +5456,8 @@ private module Impl {
or
result = getImmediateChildOfDeferStmt(e, index, partialAccessor)
or
result = getImmediateChildOfDiscardStmt(e, index, partialAccessor)
or
result = getImmediateChildOfFailStmt(e, index, partialAccessor)
or
result = getImmediateChildOfFallthroughStmt(e, index, partialAccessor)

View File

@@ -2837,6 +2837,24 @@ module Raw {
BraceStmt getBody() { defer_stmts(this, result) }
}
/**
* INTERNAL: Do not use.
* A statement that takes a non-copyable value and destructs its members/fields.
*
* The only valid syntax:
* ```
* destruct self
* ```
*/
class DiscardStmt extends @discard_stmt, Stmt {
override string toString() { result = "DiscardStmt" }
/**
* Gets the sub expression of this discard statement.
*/
Expr getSubExpr() { discard_stmts(this, result) }
}
/**
* INTERNAL: Do not use.
*/

View File

@@ -771,6 +771,10 @@ module Synth {
* INTERNAL: Do not use.
*/
TDeferStmt(Raw::DeferStmt id) { constructDeferStmt(id) } or
/**
* INTERNAL: Do not use.
*/
TDiscardStmt(Raw::DiscardStmt id) { constructDiscardStmt(id) } or
/**
* INTERNAL: Do not use.
*/
@@ -1305,9 +1309,9 @@ module Synth {
* INTERNAL: Do not use.
*/
class TStmt =
TBraceStmt or TBreakStmt or TCaseStmt or TContinueStmt or TDeferStmt or TFailStmt or
TFallthroughStmt or TLabeledStmt or TPoundAssertStmt or TReturnStmt or TThrowStmt or
TYieldStmt;
TBraceStmt or TBreakStmt or TCaseStmt or TContinueStmt or TDeferStmt or TDiscardStmt or
TFailStmt or TFallthroughStmt or TLabeledStmt or TPoundAssertStmt or TReturnStmt or
TThrowStmt or TYieldStmt;
/**
* INTERNAL: Do not use.
@@ -2796,6 +2800,13 @@ module Synth {
cached
TDeferStmt convertDeferStmtFromRaw(Raw::Element e) { result = TDeferStmt(e) }
/**
* INTERNAL: Do not use.
* Converts a raw element to a synthesized `TDiscardStmt`, if possible.
*/
cached
TDiscardStmt convertDiscardStmtFromRaw(Raw::Element e) { result = TDiscardStmt(e) }
/**
* INTERNAL: Do not use.
* Converts a raw element to a synthesized `TDoCatchStmt`, if possible.
@@ -4093,6 +4104,8 @@ module Synth {
or
result = convertDeferStmtFromRaw(e)
or
result = convertDiscardStmtFromRaw(e)
or
result = convertFailStmtFromRaw(e)
or
result = convertFallthroughStmtFromRaw(e)
@@ -5750,6 +5763,13 @@ module Synth {
cached
Raw::Element convertDeferStmtToRaw(TDeferStmt e) { e = TDeferStmt(result) }
/**
* INTERNAL: Do not use.
* Converts a synthesized `TDiscardStmt` to a raw DB element, if possible.
*/
cached
Raw::Element convertDiscardStmtToRaw(TDiscardStmt e) { e = TDiscardStmt(result) }
/**
* INTERNAL: Do not use.
* Converts a synthesized `TDoCatchStmt` to a raw DB element, if possible.
@@ -7047,6 +7067,8 @@ module Synth {
or
result = convertDeferStmtToRaw(e)
or
result = convertDiscardStmtToRaw(e)
or
result = convertFailStmtToRaw(e)
or
result = convertFallthroughStmtToRaw(e)

View File

@@ -177,6 +177,7 @@ import codeql.swift.elements.stmt.CaseStmtConstructor
import codeql.swift.elements.stmt.ConditionElementConstructor
import codeql.swift.elements.stmt.ContinueStmtConstructor
import codeql.swift.elements.stmt.DeferStmtConstructor
import codeql.swift.elements.stmt.DiscardStmtConstructor
import codeql.swift.elements.stmt.DoCatchStmtConstructor
import codeql.swift.elements.stmt.DoStmtConstructor
import codeql.swift.elements.stmt.FailStmtConstructor

View File

@@ -0,0 +1,53 @@
// generated by codegen/codegen.py
/**
* This module provides the generated definition of `DiscardStmt`.
* INTERNAL: Do not import directly.
*/
private import codeql.swift.generated.Synth
private import codeql.swift.generated.Raw
import codeql.swift.elements.expr.Expr
import codeql.swift.elements.stmt.Stmt
/**
* INTERNAL: This module contains the fully generated definition of `DiscardStmt` and should not
* be referenced directly.
*/
module Generated {
/**
* A statement that takes a non-copyable value and destructs its members/fields.
*
* The only valid syntax:
* ```
* destruct self
* ```
* INTERNAL: Do not reference the `Generated::DiscardStmt` class directly.
* Use the subclass `DiscardStmt`, where the following predicates are available.
*/
class DiscardStmt extends Synth::TDiscardStmt, Stmt {
override string getAPrimaryQlClass() { result = "DiscardStmt" }
/**
* Gets the sub expression of this discard statement.
*
* 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 getImmediateSubExpr() {
result =
Synth::convertExprFromRaw(Synth::convertDiscardStmtToRaw(this)
.(Raw::DiscardStmt)
.getSubExpr())
}
/**
* Gets the sub expression of this discard statement.
*/
final Expr getSubExpr() {
exists(Expr immediate |
immediate = this.getImmediateSubExpr() and
result = immediate.resolve()
)
}
}
}

View File

@@ -1834,6 +1834,7 @@ condition_element_availabilities( //dir=stmt
| @case_stmt
| @continue_stmt
| @defer_stmt
| @discard_stmt
| @fail_stmt
| @fallthrough_stmt
| @labeled_stmt
@@ -1921,6 +1922,11 @@ defer_stmts( //dir=stmt
int body: @brace_stmt_or_none ref
);
discard_stmts( //dir=stmt
unique int id: @discard_stmt,
int sub_expr: @expr_or_none ref
);
fail_stmts( //dir=stmt
unique int id: @fail_stmt
);

View File

@@ -0,0 +1 @@
| test.swift:4:5:4:13 | DiscardStmt | getSubExpr: | test.swift:4:13:4:13 | self |

View File

@@ -0,0 +1,10 @@
// generated by codegen/codegen.py
import codeql.swift.elements
import TestUtils
from DiscardStmt x, Expr getSubExpr
where
toBeTested(x) and
not x.isUnknown() and
getSubExpr = x.getSubExpr()
select x, "getSubExpr:", getSubExpr

View File

@@ -0,0 +1,7 @@
@_moveOnly
struct S {
__consuming func f() {
discard self
}
deinit {}
}

View File

@@ -1399,3 +1399,14 @@ class MacroDecl(GenericContext, ValueDecl):
parameters: list[ParamDecl] | doc("parameters of this macro")
roles: list[MacroRole] | doc("roles of this macro")
pass
class DiscardStmt(Stmt):
"""
A statement that takes a non-copyable value and destructs its members/fields.
The only valid syntax:
```
destruct self
```
"""
sub_expr: Expr | child