Skip to content

Commit

Permalink
Rust: add location definitions for format arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aibaars committed Oct 18, 2024
1 parent e93ebf5 commit 8653e35
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions rust/ql/lib/ide-contextual-queries/localDefinitions.ql
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,65 @@
import codeql.IDEContextual
import codeql.rust.elements.Variable
import codeql.rust.elements.Locatable
import codeql.rust.elements.FormatArgsExpr
import codeql.rust.elements.FormatArgsArg

Check warning

Code scanning / CodeQL

Redundant import Warning

Redundant import, the module is already imported inside
codeql.rust.elements.FormatArgsExpr
.
import codeql.rust.elements.NamedFormatArgument
import codeql.rust.elements.PositionalFormatArgument

external string selectedSourceFile();

predicate localVariable(Locatable e, Variable def) { e = def.getAnAccess() }
newtype TDef =
TVariable(Variable v) or
TFormatArgsArgName(Name name) { name = any(FormatArgsArg a).getName() } or
TFormatArgsArgIndex(Expr e) { e = any(FormatArgsArg a).getExpr() }

from Locatable e, Variable def, string kind
class Definition extends TDef {
predicate hasLocationInfo(string file, int startLine, int startColumn, int endLine, int endColumn) {
this.asVariable()
.getLocation()
.hasLocationInfo(file, startLine, startColumn, endLine, endColumn) or
this.asName().hasLocationInfo(file, startLine, startColumn, endLine, endColumn) or
this.asExpr().hasLocationInfo(file, startLine, startColumn, endLine, endColumn)
}

Variable asVariable() { this = TVariable(result) }

Name asName() { this = TFormatArgsArgName(result) }

Expr asExpr() { this = TFormatArgsArgIndex(result) }

string toString() {
result = this.asExpr().toString() or
result = this.asVariable().toString() or
result = this.asName().getText()
}
}

predicate localVariable(AstNode e, Variable def) { e = def.getAnAccess() }

predicate namedFormatArgument(NamedFormatArgument e, Name def) {
exists(FormatArgsExpr parent |
parent = e.getParent().getParent() and
parent.getAnArg().getName() = def and
e.getName() = def.getText()
)
}

predicate positionalFormatArgument(PositionalFormatArgument e, Expr def) {
exists(FormatArgsExpr parent |
parent = e.getParent().getParent() and
def = parent.getArg(e.getIndex()).getExpr()
)
}

from Locatable e, Definition def, string kind
where
e.getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile()) and
localVariable(e, def) and
kind = "local variable"
e.hasLocationInfo(getFileBySourceArchiveName(selectedSourceFile()).getAbsolutePath(), _, _, _, _) and
(
localVariable(e, def.asVariable()) and kind = "local variable"
or
namedFormatArgument(e, def.asName()) and kind = "format argument"
or
positionalFormatArgument(e, def.asExpr()) and kind = "format argument"
)
select e, def, kind

0 comments on commit 8653e35

Please sign in to comment.