|
| 1 | +/** |
| 2 | + * @name Use a set literal in place of `or` |
| 3 | + * @description A chain of `or`s can be replaced with a set literal, improving readability. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity recommendation |
| 6 | + * @id ql/use-set-literal |
| 7 | + * @tags maintainability |
| 8 | + * @precision high |
| 9 | + */ |
| 10 | + |
| 11 | +import ql |
| 12 | + |
| 13 | +/** |
| 14 | + * A chain of disjunctions treated as one object. For example the following is |
| 15 | + * a chain of disjunctions with three operands: |
| 16 | + * ``` |
| 17 | + * a or b or c |
| 18 | + * ``` |
| 19 | + */ |
| 20 | +class DisjunctionChain extends Disjunction { |
| 21 | + DisjunctionChain() { not exists(Disjunction parent | parent.getAnOperand() = this) } |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets any operand of the chain. |
| 25 | + */ |
| 26 | + Formula getAnOperandRec() { |
| 27 | + result = getAnOperand*() and |
| 28 | + not result instanceof Disjunction |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * An equality comparison with a `Literal`. For example: |
| 34 | + * ``` |
| 35 | + * x = 4 |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +class EqualsLiteral extends ComparisonFormula { |
| 39 | + EqualsLiteral() { |
| 40 | + getSymbol() = "=" and |
| 41 | + getAnOperand() instanceof Literal |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * A chain of disjunctions where each operand is an equality comparison between |
| 47 | + * the same thing and various `Literal`s. For example: |
| 48 | + * ``` |
| 49 | + * x = 4 or |
| 50 | + * x = 5 or |
| 51 | + * x = 6 |
| 52 | + * ``` |
| 53 | + */ |
| 54 | +class DisjunctionEqualsLiteral extends DisjunctionChain { |
| 55 | + DisjunctionEqualsLiteral() { |
| 56 | + // VarAccess on the same variable |
| 57 | + exists(VarDef v | |
| 58 | + forex(Formula f | f = getAnOperandRec() | |
| 59 | + f.(EqualsLiteral).getAnOperand().(VarAccess).getDeclaration() = v |
| 60 | + ) |
| 61 | + ) |
| 62 | + or |
| 63 | + // FieldAccess on the same variable |
| 64 | + exists(VarDecl v | |
| 65 | + forex(Formula f | f = getAnOperandRec() | |
| 66 | + f.(EqualsLiteral).getAnOperand().(FieldAccess).getDeclaration() = v |
| 67 | + ) |
| 68 | + ) |
| 69 | + or |
| 70 | + // ThisAccess |
| 71 | + forex(Formula f | f = getAnOperandRec() | |
| 72 | + f.(EqualsLiteral).getAnOperand() instanceof ThisAccess |
| 73 | + ) |
| 74 | + or |
| 75 | + // ResultAccess |
| 76 | + forex(Formula f | f = getAnOperandRec() | |
| 77 | + f.(EqualsLiteral).getAnOperand() instanceof ResultAccess |
| 78 | + ) |
| 79 | + // (in principle something like GlobalValueNumbering could be used to generalize this) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * A call with a single `Literal` argument. For example: |
| 85 | + * ``` |
| 86 | + * myPredicate(4) |
| 87 | + * ``` |
| 88 | + */ |
| 89 | +class CallLiteral extends Call { |
| 90 | + CallLiteral() { |
| 91 | + getNumberOfArguments() = 1 and |
| 92 | + getArgument(0) instanceof Literal |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * A chain of disjunctions where each operand is a call to the same predicate |
| 98 | + * using various `Literal`s. For example: |
| 99 | + * ``` |
| 100 | + * myPredicate(4) or |
| 101 | + * myPredicate(5) or |
| 102 | + * myPredicate(6) |
| 103 | + * ``` |
| 104 | + */ |
| 105 | +class DisjunctionPredicateLiteral extends DisjunctionChain { |
| 106 | + DisjunctionPredicateLiteral() { |
| 107 | + // Call to the same target |
| 108 | + exists(PredicateOrBuiltin target | |
| 109 | + forex(Formula f | f = getAnOperandRec() | f.(CallLiteral).getTarget() = target) |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +from DisjunctionChain d, string msg, int c |
| 115 | +where |
| 116 | + ( |
| 117 | + d instanceof DisjunctionEqualsLiteral and |
| 118 | + msg = |
| 119 | + "This formula of " + c.toString() + |
| 120 | + " comparisons can be replaced with a single equality on a set literal, improving readability." |
| 121 | + or |
| 122 | + d instanceof DisjunctionPredicateLiteral and |
| 123 | + msg = |
| 124 | + "This formula of " + c.toString() + |
| 125 | + " predicate calls can be replaced with a single call on a set literal, improving readability." |
| 126 | + ) and |
| 127 | + c = count(d.getAnOperandRec()) and |
| 128 | + c >= 4 |
| 129 | +select d, msg |
0 commit comments