From 33b807f3bb2dedef59e9d1760de4bd2bd1a27725 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Wed, 13 Mar 2024 16:52:06 +0000 Subject: [PATCH] Parameters and local variables: add `isAnonymous` predicate --- java/ql/lib/semmle/code/java/Expr.qll | 7 ++++++- java/ql/lib/semmle/code/java/Variable.qll | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll index 3442855a91a..315b4d19b9b 100644 --- a/java/ql/lib/semmle/code/java/Expr.qll +++ b/java/ql/lib/semmle/code/java/Expr.qll @@ -1691,6 +1691,9 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr { /** Gets the name of the variable declared by this local variable declaration expression. */ string getName() { result = this.getVariable().getName() } + /** Holds if this is an anonymous local variable, `_` */ + predicate isAnonymous() { this.getName() = "" } + /** * Gets the switch statement or expression whose pattern declares this identifier, if any. */ @@ -1763,7 +1766,9 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr { } /** Gets a printable representation of this expression. */ - override string toString() { result = this.getName() } + override string toString() { + if this.getName() = "" then result = "" else result = this.getName() + } override string getAPrimaryQlClass() { result = "LocalVariableDeclExpr" } } diff --git a/java/ql/lib/semmle/code/java/Variable.qll b/java/ql/lib/semmle/code/java/Variable.qll index 8ed650d5f16..f28e378c233 100644 --- a/java/ql/lib/semmle/code/java/Variable.qll +++ b/java/ql/lib/semmle/code/java/Variable.qll @@ -117,4 +117,11 @@ class Parameter extends Element, @param, LocalScopeVariable { } override string getAPrimaryQlClass() { result = "Parameter" } + + override string toString() { + if this.getName() = "" then result = "" else result = super.toString() + } + + /** Holds if this is an anonymous parameter, `_` */ + predicate isAnonymous() { this.getName() = "" } }