Skip to content

Swift: Add integral type classes #11841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions swift/ql/lib/codeql/swift/elements/type/FloatingPointType.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
private import swift

/**
* A floating-point type. This includes the `Float` type, the `Double`, and
* builtin floating-point types.
*/
class FloatingPointType extends Type {
FloatingPointType() {
this.getName() = ["Float", "Double"] or
this instanceof BuiltinFloatType
}
}
35 changes: 35 additions & 0 deletions swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
private import swift

/** The `Character` type. */
class CharacterType extends StructType {
CharacterType() { this.getName() = "Character" }
}

/**
* An integer-like type. For example, `Int`, `Int16`, `Uint16`, etc.
*/
class IntegerType extends Type {
IntegerType() {
this.getName() =
["Int", "Int8", "Int16", "Int32", "Int64", "UInt", "UInt8", "Uint16", "Uint32", "UInt64"]
or
this instanceof BuiltinIntegerType
}
}

/** The `Bool` type. */
class BooleanType extends Type {
BooleanType() { this.getName() = "Bool" }
}

/**
* A numeric-like type. This includes the types `Character`, `Bool`, and all
* the integer-like types.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  • isn't a floating point type numeric as well?
  • other languages don't seem to include Bool as a numeric type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I agree with both of these observations 👍

*/
class NumericOrCharType extends Type {
NumericOrCharType() {
this instanceof CharacterType or
this instanceof IntegerType or
this instanceof BooleanType
}
}
1 change: 1 addition & 0 deletions swift/ql/lib/swift.qll
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import codeql.swift.elements.expr.InitializerCallExpr
import codeql.swift.elements.expr.SelfRefExpr
import codeql.swift.elements.decl.MethodDecl
import codeql.swift.elements.decl.ClassOrStructDecl
import codeql.swift.elements.type.NumericOrCharType
import codeql.swift.Unit