Skip to content

Commit

Permalink
Swift: Add missing module Impl wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Sep 13, 2024
1 parent 288cdc5 commit 0da1611
Show file tree
Hide file tree
Showing 154 changed files with 2,157 additions and 1,835 deletions.
130 changes: 66 additions & 64 deletions swift/ql/lib/codeql/swift/elements/AstNodeImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,82 @@ private import codeql.swift.elements.expr.ClosureExpr
private import codeql.swift.elements.Callable
private import codeql.swift.generated.ParentChild

private module Cached {
private Element getEnclosingDeclStep(Element e) {
not e instanceof Decl and
result = getImmediateParent(e)
}
module Impl {
private module Cached {
private Element getEnclosingDeclStep(Element e) {
not e instanceof Decl and
result = getImmediateParent(e)
}

cached
Decl getEnclosingDecl(AstNode ast) { result = getEnclosingDeclStep*(getImmediateParent(ast)) }
cached
Decl getEnclosingDecl(AstNode ast) { result = getEnclosingDeclStep*(getImmediateParent(ast)) }

private Element getEnclosingFunctionStep(Element e) {
not e instanceof Function and
result = getEnclosingDecl(e)
}
private Element getEnclosingFunctionStep(Element e) {
not e instanceof Function and
result = getEnclosingDecl(e)
}

cached
Function getEnclosingFunction(AstNode ast) {
result = getEnclosingFunctionStep*(getEnclosingDecl(ast))
}
cached
Function getEnclosingFunction(AstNode ast) {
result = getEnclosingFunctionStep*(getEnclosingDecl(ast))
}

private Element getEnclosingClosureStep(Element e) {
not e instanceof Callable and
result = getImmediateParent(e)
}
private Element getEnclosingClosureStep(Element e) {
not e instanceof Callable and
result = getImmediateParent(e)
}

cached
ClosureExpr getEnclosingClosure(AstNode ast) {
result = getEnclosingClosureStep*(getImmediateParent(ast))
cached
ClosureExpr getEnclosingClosure(AstNode ast) {
result = getEnclosingClosureStep*(getImmediateParent(ast))
}
}
}

/**
* A node in the abstract syntax tree.
*/
class AstNode extends Generated::AstNode {
/**
* Gets the nearest function definition that contains this AST node, if any.
* This includes functions, methods, (de)initializers, and accessors, but not closures.
*
* For example, in the following code, the AST node for `n + 1` has `foo` as its
* enclosing function (via `getEnclosingFunction`), whereas its enclosing callable is
* the closure `{(n : Int) in n + 1 }` (via `getEnclosingCallable`):
*
* ```swift
* func foo() {
* var f = { (n : Int) in n + 1 }
* }
* ```
* A node in the abstract syntax tree.
*/
final Function getEnclosingFunction() { result = Cached::getEnclosingFunction(this) }
class AstNode extends Generated::AstNode {
/**
* Gets the nearest function definition that contains this AST node, if any.
* This includes functions, methods, (de)initializers, and accessors, but not closures.
*
* For example, in the following code, the AST node for `n + 1` has `foo` as its
* enclosing function (via `getEnclosingFunction`), whereas its enclosing callable is
* the closure `{(n : Int) in n + 1 }` (via `getEnclosingCallable`):
*
* ```swift
* func foo() {
* var f = { (n : Int) in n + 1 }
* }
* ```
*/
final Function getEnclosingFunction() { result = Cached::getEnclosingFunction(this) }

/**
* Gets the nearest declaration that contains this AST node, if any.
*
* Note that the nearest declaration may be an extension of a type declaration. If you always
* want the type declaration and not the extension, use `getEnclosingDecl().asNominalTypeDecl()`.
*/
final Decl getEnclosingDecl() { result = Cached::getEnclosingDecl(this) }
/**
* Gets the nearest declaration that contains this AST node, if any.
*
* Note that the nearest declaration may be an extension of a type declaration. If you always
* want the type declaration and not the extension, use `getEnclosingDecl().asNominalTypeDecl()`.
*/
final Decl getEnclosingDecl() { result = Cached::getEnclosingDecl(this) }

/**
* Gets the nearest `Callable` that contains this AST node, if any.
* This includes (auto)closures, functions, methods, (de)initializers, and accessors.
*
* For example, in the following code, the AST node for `n + 1` has the closure
* `{(n : Int) in n + 1 }` as its enclosing callable.
*
* ```swift
* func foo() {
* var f = { (n : Int) in n + 1 }
* }
* ```
*/
final Callable getEnclosingCallable() {
if exists(Cached::getEnclosingClosure(this))
then result = Cached::getEnclosingClosure(this)
else result = Cached::getEnclosingFunction(this)
/**
* Gets the nearest `Callable` that contains this AST node, if any.
* This includes (auto)closures, functions, methods, (de)initializers, and accessors.
*
* For example, in the following code, the AST node for `n + 1` has the closure
* `{(n : Int) in n + 1 }` as its enclosing callable.
*
* ```swift
* func foo() {
* var f = { (n : Int) in n + 1 }
* }
* ```
*/
final Callable getEnclosingCallable() {
if exists(Cached::getEnclosingClosure(this))
then result = Cached::getEnclosingClosure(this)
else result = Cached::getEnclosingFunction(this)
}
}
}
44 changes: 23 additions & 21 deletions swift/ql/lib/codeql/swift/elements/AvailabilityInfoImpl.qll
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
private import codeql.swift.generated.AvailabilityInfo

// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* An availability condition of an `if`, `while`, or `guard` statements.
*
* Examples:
* ```
* if #available(iOS 12, *) {
* // Runs on iOS 12 and above
* } else {
* // Runs only anything below iOS 12
* }
* if #unavailable(macOS 10.14, *) {
* // Runs only on macOS 10 and below
* }
* ```
*/
class AvailabilityInfo extends Generated::AvailabilityInfo {
override string toString() {
result = "#available" and not this.isUnavailable()
or
result = "#unavailable" and this.isUnavailable()
module Impl {
// the following QLdoc is generated: if you need to edit it, do it in the schema file
/**
* An availability condition of an `if`, `while`, or `guard` statements.
*
* Examples:
* ```
* if #available(iOS 12, *) {
* // Runs on iOS 12 and above
* } else {
* // Runs only anything below iOS 12
* }
* if #unavailable(macOS 10.14, *) {
* // Runs only on macOS 10 and below
* }
* ```
*/
class AvailabilityInfo extends Generated::AvailabilityInfo {
override string toString() {
result = "#available" and not this.isUnavailable()
or
result = "#unavailable" and this.isUnavailable()
}
}
}
26 changes: 14 additions & 12 deletions swift/ql/lib/codeql/swift/elements/CallableImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ private import codeql.swift.generated.Callable
private import codeql.swift.elements.AstNode
private import codeql.swift.elements.decl.Decl

class Callable extends Generated::Callable, AstNode {
/**
* Holds if this Callable is a function named `funcName`.
*/
predicate hasName(string funcName) { this.getName() = funcName }
module Impl {
class Callable extends Generated::Callable {
/**
* Holds if this Callable is a function named `funcName`.
*/
predicate hasName(string funcName) { this.getName() = funcName }

/**
* Holds if this Callable is a function named `funcName` defined in a module
* called `moduleName`.
*/
predicate hasName(string moduleName, string funcName) {
this.hasName(funcName) and
this.(Decl).getModule().getFullName() = moduleName
/**
* Holds if this Callable is a function named `funcName` defined in a module
* called `moduleName`.
*/
predicate hasName(string moduleName, string funcName) {
this.hasName(funcName) and
this.(Decl).getModule().getFullName() = moduleName
}
}
}
50 changes: 26 additions & 24 deletions swift/ql/lib/codeql/swift/elements/CommentImpl.qll
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
private import codeql.swift.generated.Comment

class Comment extends Generated::Comment {
/** toString */
override string toString() { result = this.getText() }
}
module Impl {
class Comment extends Generated::Comment {
/** toString */
override string toString() { result = this.getText() }
}

class SingleLineComment extends Comment {
SingleLineComment() {
this.getText().matches("//%") and
not this instanceof SingleLineDocComment
class SingleLineComment extends Comment {
SingleLineComment() {
this.getText().matches("//%") and
not this instanceof SingleLineDocComment
}
}
}

class MultiLineComment extends Comment {
MultiLineComment() {
this.getText().matches("/*%") and
not this instanceof MultiLineDocComment
class MultiLineComment extends Comment {
MultiLineComment() {
this.getText().matches("/*%") and
not this instanceof MultiLineDocComment
}
}
}

class DocComment extends Comment {
DocComment() {
this instanceof SingleLineDocComment or
this instanceof MultiLineDocComment
class DocComment extends Comment {
DocComment() {
this instanceof SingleLineDocComment or
this instanceof MultiLineDocComment
}
}
}

class SingleLineDocComment extends Comment {
SingleLineDocComment() { this.getText().matches("///%") }
}
class SingleLineDocComment extends Comment {
SingleLineDocComment() { this.getText().matches("///%") }
}

class MultiLineDocComment extends Comment {
MultiLineDocComment() { this.getText().matches("/**%") }
class MultiLineDocComment extends Comment {
MultiLineDocComment() { this.getText().matches("/**%") }
}
}
80 changes: 41 additions & 39 deletions swift/ql/lib/codeql/swift/elements/DiagnosticsImpl.qll
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
private import codeql.swift.generated.Diagnostics

/**
* A compiler-generated error, warning, note or remark.
*/
class Diagnostics extends Generated::Diagnostics {
override string toString() { result = this.getSeverity() + ": " + this.getText() }

module Impl {
/**
* Gets a string representing the severity of this compiler diagnostic.
* A compiler-generated error, warning, note or remark.
*/
string getSeverity() {
this.getKind() = 1 and result = "error"
or
this.getKind() = 2 and result = "warning"
or
this.getKind() = 3 and result = "note"
or
this.getKind() = 4 and result = "remark"
class Diagnostics extends Generated::Diagnostics {
override string toString() { result = this.getSeverity() + ": " + this.getText() }

/**
* Gets a string representing the severity of this compiler diagnostic.
*/
string getSeverity() {
this.getKind() = 1 and result = "error"
or
this.getKind() = 2 and result = "warning"
or
this.getKind() = 3 and result = "note"
or
this.getKind() = 4 and result = "remark"
}
}
}

/**
* A compiler error message.
*/
class CompilerError extends Diagnostics {
CompilerError() { this.getSeverity() = "error" }
}
/**
* A compiler error message.
*/
class CompilerError extends Diagnostics {
CompilerError() { this.getSeverity() = "error" }
}

/**
* A compiler-generated warning.
*/
class CompilerWarning extends Diagnostics {
CompilerWarning() { this.getSeverity() = "warning" }
}
/**
* A compiler-generated warning.
*/
class CompilerWarning extends Diagnostics {
CompilerWarning() { this.getSeverity() = "warning" }
}

/**
* A compiler-generated note (typically attached to an error or warning).
*/
class CompilerNote extends Diagnostics {
CompilerNote() { this.getSeverity() = "note" }
}
/**
* A compiler-generated note (typically attached to an error or warning).
*/
class CompilerNote extends Diagnostics {
CompilerNote() { this.getSeverity() = "note" }
}

/**
* A compiler-generated remark (milder than a warning, this does not indicate an issue).
*/
class CompilerRemark extends Diagnostics {
CompilerRemark() { this.getSeverity() = "remark" }
/**
* A compiler-generated remark (milder than a warning, this does not indicate an issue).
*/
class CompilerRemark extends Diagnostics {
CompilerRemark() { this.getSeverity() = "remark" }
}
}
Loading

0 comments on commit 0da1611

Please sign in to comment.