Java: Add Expr.isParenthesized, adjust VarAccess.toString, and fix tests.

This commit is contained in:
Anders Schack-Mulligen
2020-01-21 15:52:09 +01:00
parent 597d8e7d94
commit 4bd332ddca
5 changed files with 14 additions and 6 deletions

View File

@@ -13,7 +13,10 @@ import java
predicate nontrivialLogicalOperator(BinaryExpr e) {
e instanceof LogicExpr and
not e.getParent().(Expr).getKind() = e.getKind()
(
not e.getParent().(Expr).getKind() = e.getKind() or
e.isParenthesized()
)
}
Expr getSimpleParent(Expr e) {

View File

@@ -95,6 +95,9 @@ class Expr extends ExprParent, @expr {
or
exists(LambdaExpr lam | lam.asMethod() = getEnclosingCallable() and lam.isInStaticContext())
}
/** Holds if this expression is parenthesized. */
predicate isParenthesized() { isParenthesized(this, _) }
}
/**
@@ -1330,7 +1333,11 @@ class VarAccess extends Expr, @varaccess {
/** Gets a printable representation of this expression. */
override string toString() {
result = this.getQualifier().toString() + "." + this.getVariable().getName()
exists(Expr q | q = this.getQualifier() |
if q.isParenthesized()
then result = "(...)." + this.getVariable().getName()
else result = q.toString() + "." + this.getVariable().getName()
)
or
not this.hasQualifier() and result = this.getVariable().getName()
}