Swift: extract InoutType

This commit is contained in:
Paolo Tranquilli
2022-06-24 17:29:46 +02:00
parent 189a47e30d
commit fd209e57cd
9 changed files with 45 additions and 5 deletions

View File

@@ -100,6 +100,7 @@ ErrorType:
InOutType:
_extends: Type
object_type: Type
LValueType:
_extends: Type

View File

@@ -271,4 +271,11 @@ codeql::VariadicSequenceType TypeVisitor::translateVariadicSequenceType(
return entry;
}
codeql::InOutType TypeVisitor::translateInOutType(const swift::InOutType& type) {
codeql::InOutType entry{dispatcher_.assignNewLabel(type)};
entry.object_type = dispatcher_.fetchLabel(type.getObjectType());
fillType(type, entry);
return entry;
}
} // namespace codeql

View File

@@ -42,6 +42,7 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
codeql::DynamicSelfType translateDynamicSelfType(const swift::DynamicSelfType& type);
codeql::VariadicSequenceType translateVariadicSequenceType(
const swift::VariadicSequenceType& type);
codeql::InOutType translateInOutType(const swift::InOutType& type);
private:
void fillType(const swift::TypeBase& type, codeql::Type& entry);

View File

@@ -3,4 +3,11 @@ import codeql.swift.elements.type.Type
class InOutTypeBase extends @in_out_type, Type {
override string getAPrimaryQlClass() { result = "InOutType" }
Type getObjectType() {
exists(Type x |
in_out_types(this, x) and
result = x.resolve()
)
}
}

View File

@@ -253,7 +253,8 @@ error_types( //dir=type
);
in_out_types( //dir=type
unique int id: @in_out_type
unique int id: @in_out_type,
int object_type: @type ref
);
l_value_types( //dir=type

View File

@@ -0,0 +1,2 @@
| inout Int | getDiagnosticsName: | inout Int | getCanonicalType: | inout Int | getObjectType: | Int |
| inout S | getDiagnosticsName: | inout S | getCanonicalType: | inout S | getObjectType: | S |

View File

@@ -0,0 +1,13 @@
// generated by codegen/codegen.py
import codeql.swift.elements
import TestUtils
from InOutType x, string getDiagnosticsName, Type getCanonicalType, Type getObjectType
where
toBeTested(x) and
not x.isUnknown() and
getDiagnosticsName = x.getDiagnosticsName() and
getCanonicalType = x.getCanonicalType() and
getObjectType = x.getObjectType()
select x, "getDiagnosticsName:", getDiagnosticsName, "getCanonicalType:", getCanonicalType,
"getObjectType:", getObjectType

View File

@@ -1,4 +0,0 @@
// generated by codegen/codegen.py
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
will appear and this file will be deleted

View File

@@ -0,0 +1,12 @@
func foo(_: inout Int) {}
var x: Int = 42
foo(&x)
struct S {
mutating func bar() {}
}
var s: S
s.bar()