Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions unified/ql/lib/codeql/unified/internal/AstPlugin.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
overlay[local]
module;

private import unified
private import codeql.util.Unit

private module Plugins {
private import AstPluginSwift
}

class AstPlugin extends Unit {
bindingset[f]
string getFunctionDeclarationKeyword(FunctionDeclaration f) { none() }

bindingset[c]
string getConstructorDeclarationKeyword(ConstructorDeclaration c) { none() }

bindingset[cls]
string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) { none() }

bindingset[decl]
string getVariableDeclarationKeyword(VariableDeclaration decl) { none() }
Comment on lines +12 to +22

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it perhaps we worth simplifying this to something like?

predicate includeModifierInToString(Modifier m);

and then the language plugin can decide if it's worth checking the node type or just the modifier text.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think we extract func modifiers, so we would then have to do that.

}

bindingset[f]
string getFunctionDeclarationKeyword(FunctionDeclaration f) {
result = any(AstPlugin p).getFunctionDeclarationKeyword(f)
}

bindingset[c]
string getConstructorDeclarationKeyword(ConstructorDeclaration c) {
result = any(AstPlugin p).getConstructorDeclarationKeyword(c)
}

bindingset[cls]
string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) {
result = any(AstPlugin p).getClassLikeDeclarationKeyword(cls)
}

bindingset[decl]
string getVariableDeclarationKeyword(VariableDeclaration decl) {
result = any(AstPlugin p).getVariableDeclarationKeyword(decl)
}
22 changes: 22 additions & 0 deletions unified/ql/lib/codeql/unified/internal/AstPluginSwift.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
overlay[local]
module;

private import unified
private import AstPlugin

private class AstPluginSwift extends AstPlugin {
bindingset[f]
override string getFunctionDeclarationKeyword(FunctionDeclaration f) {
exists(f) and result = "func"
}

override string getClassLikeDeclarationKeyword(ClassLikeDeclaration cls) {
cls.hasModifier(result) and
result in ["class", "struct", "enum", "actor", "extension", "protocol"]
}

override string getVariableDeclarationKeyword(VariableDeclaration decl) {
decl.hasModifier(result) and
result in ["var", "let"]
}
}
28 changes: 23 additions & 5 deletions unified/ql/lib/codeql/unified/internal/FacadeAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
overlay[local?]
module;

private import AstPlugin
private import codeql.files.FileSystem
private import codeql.unified.internal.NameBinding as NameBinding

Expand Down Expand Up @@ -153,17 +154,18 @@
NameBinding::getStaticBindingTargetFromRef(this.getABaseType().getType())
}

private string getKind() {
this.hasModifier(result) and
result in ["class", "struct", "interface", "enum", "actor", "extension", "protocol"]
override string toString() {
result = concat(getClassLikeDeclarationKeyword(this) + " ") + concat(this.getName())
}

override string toString() { result = concat(this.getKind() + " ") + concat(this.getName()) }
}

class ConstructorDeclaration extends G::ConstructorDeclaration {
/** Gets the name of this constructor. */
string getName() { result = this.getNameNode().getValue() }

override string toString() {
result = concat(getConstructorDeclarationKeyword(this) + " ") + concat(this.getName())
}
}

class ContinueExpr extends G::ContinueExpr {
Expand All @@ -174,6 +176,22 @@
class FunctionDeclaration extends G::FunctionDeclaration {
/** Gets the name of this function. */
string getName() { result = this.getNameNode().getValue() }

override string toString() {
result = concat(getFunctionDeclarationKeyword(this) + " ") + concat(this.getName())
}
}

class VariableDeclaration extends G::VariableDeclaration {

Check warning on line 185 in unified/ql/lib/codeql/unified/internal/FacadeAst.qll

View workflow job for this annotation

GitHub Actions / qldoc

Missing QLdoc for class FacadeAst::Unified::VariableDeclaration
/** Gets the name node of this variable declaration, if any. */
Identifier getNameNode() { result = this.getPattern() }

/** Gets the name of the variable being declared, if any. */
string getName() { result = this.getNameNode().getValue() }

override string toString() {
result = concat(getVariableDeclarationKeyword(this) + " ") + concat(this.getName())
}
}

class LabeledStmt extends G::LabeledStmt {
Expand Down
Loading
Loading