From 885137dca22b4f14c1a0fe2a606ac0e387bb32bd Mon Sep 17 00:00:00 2001 From: Nick Rolfe Date: Thu, 11 Feb 2021 15:29:42 +0000 Subject: [PATCH] Simplify representation of calls that use scope resolution operator. Now, `Foo::bar` is a call where the receiver expr is `Foo`. --- ql/src/codeql_ruby/ast/Call.qll | 48 ++----- ql/src/codeql_ruby/ast/internal/Call.qll | 43 +----- .../library-tests/ast/calls/calls.expected | 130 ++++-------------- ql/test/library-tests/ast/calls/calls.ql | 4 - 4 files changed, 44 insertions(+), 181 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Call.qll b/ql/src/codeql_ruby/ast/Call.qll index acc77bf2012..38d569e2021 100644 --- a/ql/src/codeql_ruby/ast/Call.qll +++ b/ql/src/codeql_ruby/ast/Call.qll @@ -11,12 +11,16 @@ class Call extends Expr { /** * Gets the receiver of this call, if any. For example: + * * ```rb * foo.bar - * baz() + * Baz::qux + * corge() * ``` - * The result for the call to `bar` is the `Expr` for `foo`, while the call - * to `baz` has no result. + * + * The result for the call to `bar` is the `Expr` for `foo`; the result for + * the call to `qux` is the `Expr` for `Baz`; for the call to `corge` there + * is no result. */ final Expr getReceiver() { result = range.getReceiver() } @@ -26,47 +30,11 @@ class Call extends Expr { * ```rb * foo.bar x, y * ``` + * * the result is `"bar"`. - * - * N.B. in the following example, where the method name uses the scope - * resolution operator, the result is the name being resolved, i.e. `"bar"`. - * Use `getMethodNameScopeExpr` to get the expression for `Foo`. - * - * ```rb - * Foo::bar x, y - * ``` */ final string getMethodName() { result = range.getMethodName() } - /** - * Gets the scope expression used in the method name's scope resolution - * operation, if any. - * - * In the following example, the result is the `Expr` for `Foo`. - * - * ```rb - * Foo::bar() - * ``` - * - * However, there is no result for the following example, since there is no - * scope resolution operation. - * - * ```rb - * baz() - * ``` - */ - final Expr getMethodNameScopeExpr() { result = range.getMethodNameScopeExpr() } - - /** - * Holds if the method name uses the scope resolution operator to access the - * global scope, as in this example: - * - * ```rb - * ::foo - * ``` - */ - final predicate methodNameHasGlobalScope() { range.methodNameHasGlobalScope() } - /** * Gets the `n`th argument of this method call. In the following example, the * result for n=0 is the `IntegerLiteral` 0, while for n=1 the result is a diff --git a/ql/src/codeql_ruby/ast/internal/Call.qll b/ql/src/codeql_ruby/ast/internal/Call.qll index e2da0f0792b..48f64a6cfc6 100644 --- a/ql/src/codeql_ruby/ast/internal/Call.qll +++ b/ql/src/codeql_ruby/ast/internal/Call.qll @@ -9,10 +9,6 @@ module Call { abstract string getMethodName(); - abstract Expr getMethodNameScopeExpr(); - - abstract predicate methodNameHasGlobalScope(); - abstract Expr getArgument(int n); abstract Block getBlock(); @@ -29,10 +25,6 @@ module Call { final override string getMethodName() { result = generated.getValue() } - final override Expr getMethodNameScopeExpr() { none() } - - final override predicate methodNameHasGlobalScope() { none() } - final override Expr getArgument(int n) { none() } final override Block getBlock() { none() } @@ -48,14 +40,10 @@ module Call { not access(identifier, _) } - final override Expr getReceiver() { none() } + final override Expr getReceiver() { result = generated.getScope() } final override string getMethodName() { result = identifier.getValue() } - final override Expr getMethodNameScopeExpr() { result = generated.getScope() } - - final override predicate methodNameHasGlobalScope() { not exists(generated.getScope()) } - final override Expr getArgument(int n) { none() } final override Block getBlock() { none() } @@ -64,7 +52,11 @@ module Call { private class RegularCallRange extends Call::Range, @call { final override Generated::Call generated; - final override Expr getReceiver() { result = generated.getReceiver() } + final override Expr getReceiver() { + if exists(generated.getReceiver()) + then result = generated.getReceiver() + else result = generated.getMethod().(Generated::ScopeResolution).getScope() + } final override string getMethodName() { result = generated.getMethod().(Generated::Token).getValue() or @@ -72,17 +64,6 @@ module Call { generated.getMethod().(Generated::ScopeResolution).getName().(Generated::Token).getValue() } - final override Expr getMethodNameScopeExpr() { - result = generated.getMethod().(Generated::ScopeResolution).getScope() - } - - final override predicate methodNameHasGlobalScope() { - exists(Generated::ScopeResolution sr | - sr = generated.getMethod() and - not exists(sr.getScope()) - ) - } - final override Expr getArgument(int n) { result = generated.getArguments().getChild(n) } final override Block getBlock() { result = generated.getBlock() } @@ -97,10 +78,6 @@ module YieldCall { final override string getMethodName() { result = "yield" } - final override Expr getMethodNameScopeExpr() { none() } - - final override predicate methodNameHasGlobalScope() { none() } - final override Expr getArgument(int n) { result = generated.getChild().getChild(n) } final override Block getBlock() { none() } @@ -121,10 +98,6 @@ module SuperCall { final override string getMethodName() { result = generated.getValue() } - final override Expr getMethodNameScopeExpr() { none() } - - final override predicate methodNameHasGlobalScope() { none() } - final override Expr getArgument(int n) { none() } final override Block getBlock() { none() } @@ -141,10 +114,6 @@ module SuperCall { result = generated.getMethod().(Generated::Super).getValue() } - final override Expr getMethodNameScopeExpr() { none() } - - final override predicate methodNameHasGlobalScope() { none() } - final override Expr getArgument(int n) { result = generated.getArguments().getChild(n) } final override Block getBlock() { result = generated.getBlock() } diff --git a/ql/test/library-tests/ast/calls/calls.expected b/ql/test/library-tests/ast/calls/calls.expected index f987caefca0..893e3fb4e99 100644 --- a/ql/test/library-tests/ast/calls/calls.expected +++ b/ql/test/library-tests/ast/calls/calls.expected @@ -1,150 +1,110 @@ callsWithNoReceiverArgumentsOrBlock | calls.rb:2:1:2:5 | call to foo | foo | -| calls.rb:5:1:5:10 | call to bar | bar | | calls.rb:8:1:8:7 | call to bar | bar | | calls.rb:31:3:31:7 | call to yield | yield | | calls.rb:46:1:46:3 | call to foo | foo | -| calls.rb:47:1:47:6 | call to foo | foo | | calls.rb:50:2:50:4 | call to foo | foo | -| calls.rb:51:2:51:7 | call to foo | foo | | calls.rb:54:11:54:13 | call to foo | foo | -| calls.rb:55:11:55:16 | call to foo | foo | | calls.rb:58:2:58:4 | call to foo | foo | -| calls.rb:59:2:59:7 | call to foo | foo | | calls.rb:62:8:62:10 | call to foo | foo | -| calls.rb:63:8:63:13 | call to foo | foo | | calls.rb:66:9:66:11 | call to bar | bar | -| calls.rb:67:9:67:14 | call to bar | bar | | calls.rb:70:8:70:10 | call to foo | foo | -| calls.rb:70:13:70:18 | call to bar | bar | | calls.rb:74:3:74:5 | call to foo | foo | -| calls.rb:75:3:75:8 | call to foo | foo | | calls.rb:79:9:79:11 | call to foo | foo | -| calls.rb:79:14:79:19 | call to bar | bar | | calls.rb:82:7:82:9 | call to foo | foo | -| calls.rb:82:12:82:17 | call to bar | bar | | calls.rb:85:1:85:3 | call to foo | foo | -| calls.rb:85:7:85:12 | call to bar | bar | | calls.rb:88:2:88:4 | call to foo | foo | -| calls.rb:89:2:89:7 | call to bar | bar | | calls.rb:92:9:92:11 | call to bar | bar | -| calls.rb:92:14:92:19 | call to baz | baz | | calls.rb:96:3:96:5 | call to bar | bar | -| calls.rb:97:3:97:8 | call to baz | baz | | calls.rb:101:1:101:3 | call to foo | foo | | calls.rb:102:1:102:3 | call to bar | bar | | calls.rb:106:6:106:8 | call to foo | foo | | calls.rb:107:6:107:8 | call to bar | bar | | calls.rb:108:3:108:5 | call to baz | baz | -| calls.rb:110:6:110:11 | call to foo | foo | -| calls.rb:111:6:111:11 | call to bar | bar | -| calls.rb:112:3:112:8 | call to baz | baz | | calls.rb:117:3:117:5 | call to foo | foo | -| calls.rb:118:3:118:8 | call to bar | bar | | calls.rb:122:17:122:19 | call to foo | foo | -| calls.rb:124:18:124:23 | call to foo | foo | | calls.rb:128:10:128:12 | call to foo | foo | | calls.rb:129:3:129:5 | call to bar | bar | -| calls.rb:131:10:131:15 | call to foo | foo | -| calls.rb:132:3:132:8 | call to bar | bar | | calls.rb:137:3:137:5 | call to foo | foo | -| calls.rb:138:3:138:8 | call to bar | bar | | calls.rb:142:5:142:7 | call to foo | foo | | calls.rb:143:3:143:5 | call to bar | bar | -| calls.rb:144:3:144:8 | call to baz | baz | | calls.rb:148:40:148:42 | call to foo | foo | -| calls.rb:150:41:150:46 | call to foo | foo | | calls.rb:154:40:154:42 | call to foo | foo | -| calls.rb:156:41:156:46 | call to foo | foo | | calls.rb:161:3:161:5 | call to foo | foo | -| calls.rb:162:3:162:8 | call to bar | bar | | calls.rb:166:1:166:3 | call to foo | foo | | calls.rb:166:7:166:9 | call to bar | bar | | calls.rb:166:13:166:15 | call to baz | baz | -| calls.rb:167:1:167:6 | call to foo | foo | -| calls.rb:167:10:167:15 | call to bar | bar | -| calls.rb:167:19:167:24 | call to baz | baz | | calls.rb:170:4:170:6 | call to foo | foo | | calls.rb:171:3:171:8 | call to wibble | wibble | | calls.rb:172:7:172:9 | call to bar | bar | | calls.rb:173:3:173:8 | call to wobble | wobble | | calls.rb:175:3:175:8 | call to wabble | wabble | -| calls.rb:177:4:177:9 | call to foo | foo | -| calls.rb:178:3:178:11 | call to wibble | wibble | -| calls.rb:179:7:179:12 | call to bar | bar | -| calls.rb:180:3:180:11 | call to wobble | wobble | -| calls.rb:182:3:182:11 | call to wabble | wabble | | calls.rb:186:1:186:3 | call to bar | bar | | calls.rb:186:8:186:10 | call to foo | foo | -| calls.rb:187:1:187:6 | call to bar | bar | -| calls.rb:187:11:187:16 | call to foo | foo | | calls.rb:190:8:190:10 | call to foo | foo | | calls.rb:191:3:191:5 | call to bar | bar | -| calls.rb:193:8:193:13 | call to foo | foo | -| calls.rb:194:3:194:8 | call to bar | bar | | calls.rb:198:1:198:3 | call to bar | bar | | calls.rb:198:12:198:14 | call to foo | foo | -| calls.rb:199:1:199:6 | call to bar | bar | -| calls.rb:199:15:199:20 | call to foo | foo | | calls.rb:202:7:202:9 | call to foo | foo | | calls.rb:203:3:203:5 | call to bar | bar | -| calls.rb:205:7:205:12 | call to foo | foo | -| calls.rb:206:3:206:8 | call to bar | bar | | calls.rb:210:1:210:3 | call to bar | bar | | calls.rb:210:11:210:13 | call to foo | foo | -| calls.rb:211:1:211:6 | call to bar | bar | -| calls.rb:211:14:211:19 | call to foo | foo | | calls.rb:214:7:214:9 | call to foo | foo | | calls.rb:215:3:215:5 | call to bar | bar | -| calls.rb:217:7:217:12 | call to foo | foo | -| calls.rb:218:3:218:8 | call to bar | bar | | calls.rb:222:1:222:3 | call to bar | bar | | calls.rb:222:11:222:13 | call to foo | foo | -| calls.rb:223:1:223:6 | call to bar | bar | -| calls.rb:223:14:223:19 | call to foo | foo | | calls.rb:226:10:226:12 | call to bar | bar | | calls.rb:227:3:227:5 | call to baz | baz | -| calls.rb:229:10:229:15 | call to bar | bar | -| calls.rb:230:3:230:8 | call to baz | baz | | calls.rb:234:1:234:3 | call to foo | foo | | calls.rb:234:5:234:7 | call to bar | bar | -| calls.rb:235:1:235:6 | call to foo | foo | -| calls.rb:235:8:235:13 | call to bar | bar | | calls.rb:238:8:238:10 | call to bar | bar | -| calls.rb:238:15:238:20 | call to baz | baz | | calls.rb:241:1:241:3 | call to foo | foo | -| calls.rb:242:1:242:6 | call to foo | foo | | calls.rb:245:1:245:3 | call to foo | foo | | calls.rb:245:6:245:8 | call to bar | bar | -| calls.rb:246:1:246:6 | call to foo | foo | -| calls.rb:246:9:246:14 | call to bar | bar | | calls.rb:249:3:249:5 | call to foo | foo | | calls.rb:249:10:249:12 | call to bar | bar | -| calls.rb:249:15:249:20 | call to foo | foo | -| calls.rb:249:25:249:30 | call to bar | bar | | calls.rb:253:8:253:10 | call to foo | foo | | calls.rb:254:8:254:10 | call to bar | bar | -| calls.rb:257:8:257:13 | call to foo | foo | -| calls.rb:258:8:258:13 | call to bar | bar | | calls.rb:262:1:262:3 | call to foo | foo | | calls.rb:262:12:262:14 | call to bar | bar | -| calls.rb:263:1:263:6 | call to foo | foo | -| calls.rb:263:15:263:20 | call to bar | bar | | calls.rb:266:6:266:8 | call to bar | bar | -| calls.rb:267:6:267:11 | call to bar | bar | | calls.rb:270:6:270:8 | call to bar | bar | -| calls.rb:271:6:271:11 | call to bar | bar | | calls.rb:274:7:274:9 | call to bar | bar | -| calls.rb:275:7:275:12 | call to bar | bar | | calls.rb:278:11:278:13 | call to bar | bar | -| calls.rb:279:11:279:16 | call to bar | bar | | calls.rb:286:5:286:9 | call to super | super | | calls.rb:287:5:287:11 | call to super | super | | calls.rb:303:5:303:7 | call to foo | foo | | calls.rb:304:5:304:14 | call to super | super | | calls.rb:305:5:305:9 | call to super | super | -callsWithNameScopeExpr +callsWithArguments +| calls.rb:14:1:14:11 | call to foo | foo | 0 | calls.rb:14:5:14:5 | 0 | +| calls.rb:14:1:14:11 | call to foo | foo | 1 | calls.rb:14:8:14:8 | 1 | +| calls.rb:14:1:14:11 | call to foo | foo | 2 | calls.rb:14:11:14:11 | 2 | +| calls.rb:25:1:27:3 | call to bar | bar | 0 | calls.rb:25:9:25:13 | foo | +| calls.rb:36:3:36:16 | call to yield | yield | 0 | calls.rb:36:9:36:11 | 100 | +| calls.rb:36:3:36:16 | call to yield | yield | 1 | calls.rb:36:14:36:16 | 200 | +| calls.rb:54:1:54:14 | call to some_func | some_func | 0 | calls.rb:54:11:54:13 | call to foo | +| calls.rb:55:1:55:17 | call to some_func | some_func | 0 | calls.rb:55:11:55:16 | call to foo | +| calls.rb:266:1:266:9 | call to foo | foo | 0 | calls.rb:266:5:266:8 | &... | +| calls.rb:267:1:267:12 | call to foo | foo | 0 | calls.rb:267:5:267:11 | &... | +| calls.rb:270:1:270:9 | call to foo | foo | 0 | calls.rb:270:5:270:8 | *... | +| calls.rb:271:1:271:12 | call to foo | foo | 0 | calls.rb:271:5:271:11 | *... | +| calls.rb:274:1:274:10 | call to foo | foo | 0 | calls.rb:274:5:274:9 | **... | +| calls.rb:275:1:275:13 | call to foo | foo | 0 | calls.rb:275:5:275:12 | **... | +| calls.rb:278:1:278:14 | call to foo | foo | 0 | calls.rb:278:5:278:13 | Pair | +| calls.rb:279:1:279:17 | call to foo | foo | 0 | calls.rb:279:5:279:16 | Pair | +| calls.rb:288:5:288:16 | call to super | super | 0 | calls.rb:288:11:288:16 | blah | +| calls.rb:289:5:289:17 | call to super | super | 0 | calls.rb:289:11:289:11 | 1 | +| calls.rb:289:5:289:17 | call to super | super | 1 | calls.rb:289:14:289:14 | 2 | +| calls.rb:289:5:289:17 | call to super | super | 2 | calls.rb:289:17:289:17 | 3 | +| calls.rb:292:5:292:30 | call to super | super | 0 | calls.rb:292:11:292:11 | 4 | +| calls.rb:292:5:292:30 | call to super | super | 1 | calls.rb:292:14:292:14 | 5 | +| calls.rb:293:5:293:33 | call to super | super | 0 | calls.rb:293:11:293:11 | 6 | +| calls.rb:293:5:293:33 | call to super | super | 1 | calls.rb:293:14:293:14 | 7 | +callsWithReceiver | calls.rb:5:1:5:10 | call to bar | calls.rb:5:1:5:3 | Foo | +| calls.rb:11:1:11:7 | call to bar | calls.rb:11:1:11:3 | 123 | +| calls.rb:25:1:27:3 | call to bar | calls.rb:25:1:25:3 | 123 | | calls.rb:47:1:47:6 | call to foo | calls.rb:47:1:47:1 | X | | calls.rb:51:2:51:7 | call to foo | calls.rb:51:2:51:2 | X | | calls.rb:55:11:55:16 | call to foo | calls.rb:55:11:55:11 | X | @@ -159,6 +119,8 @@ callsWithNameScopeExpr | calls.rb:89:2:89:7 | call to bar | calls.rb:89:2:89:2 | X | | calls.rb:92:14:92:19 | call to baz | calls.rb:92:14:92:14 | X | | calls.rb:97:3:97:8 | call to baz | calls.rb:97:3:97:3 | X | +| calls.rb:101:1:101:9 | call to bar | calls.rb:101:1:101:3 | call to foo | +| calls.rb:102:1:102:9 | call to baz | calls.rb:102:1:102:3 | call to bar | | calls.rb:110:6:110:11 | call to foo | calls.rb:110:6:110:6 | X | | calls.rb:111:6:111:11 | call to bar | calls.rb:111:6:111:6 | X | | calls.rb:112:3:112:8 | call to baz | calls.rb:112:3:112:3 | X | @@ -211,38 +173,6 @@ callsWithNameScopeExpr | calls.rb:271:6:271:11 | call to bar | calls.rb:271:6:271:6 | X | | calls.rb:275:7:275:12 | call to bar | calls.rb:275:7:275:7 | X | | calls.rb:279:11:279:16 | call to bar | calls.rb:279:11:279:11 | X | -callsWithGlobalNameScopeExpr -| calls.rb:8:1:8:7 | call to bar | -callsWithArguments -| calls.rb:14:1:14:11 | call to foo | foo | 0 | calls.rb:14:5:14:5 | 0 | -| calls.rb:14:1:14:11 | call to foo | foo | 1 | calls.rb:14:8:14:8 | 1 | -| calls.rb:14:1:14:11 | call to foo | foo | 2 | calls.rb:14:11:14:11 | 2 | -| calls.rb:25:1:27:3 | call to bar | bar | 0 | calls.rb:25:9:25:13 | foo | -| calls.rb:36:3:36:16 | call to yield | yield | 0 | calls.rb:36:9:36:11 | 100 | -| calls.rb:36:3:36:16 | call to yield | yield | 1 | calls.rb:36:14:36:16 | 200 | -| calls.rb:54:1:54:14 | call to some_func | some_func | 0 | calls.rb:54:11:54:13 | call to foo | -| calls.rb:55:1:55:17 | call to some_func | some_func | 0 | calls.rb:55:11:55:16 | call to foo | -| calls.rb:266:1:266:9 | call to foo | foo | 0 | calls.rb:266:5:266:8 | &... | -| calls.rb:267:1:267:12 | call to foo | foo | 0 | calls.rb:267:5:267:11 | &... | -| calls.rb:270:1:270:9 | call to foo | foo | 0 | calls.rb:270:5:270:8 | *... | -| calls.rb:271:1:271:12 | call to foo | foo | 0 | calls.rb:271:5:271:11 | *... | -| calls.rb:274:1:274:10 | call to foo | foo | 0 | calls.rb:274:5:274:9 | **... | -| calls.rb:275:1:275:13 | call to foo | foo | 0 | calls.rb:275:5:275:12 | **... | -| calls.rb:278:1:278:14 | call to foo | foo | 0 | calls.rb:278:5:278:13 | Pair | -| calls.rb:279:1:279:17 | call to foo | foo | 0 | calls.rb:279:5:279:16 | Pair | -| calls.rb:288:5:288:16 | call to super | super | 0 | calls.rb:288:11:288:16 | blah | -| calls.rb:289:5:289:17 | call to super | super | 0 | calls.rb:289:11:289:11 | 1 | -| calls.rb:289:5:289:17 | call to super | super | 1 | calls.rb:289:14:289:14 | 2 | -| calls.rb:289:5:289:17 | call to super | super | 2 | calls.rb:289:17:289:17 | 3 | -| calls.rb:292:5:292:30 | call to super | super | 0 | calls.rb:292:11:292:11 | 4 | -| calls.rb:292:5:292:30 | call to super | super | 1 | calls.rb:292:14:292:14 | 5 | -| calls.rb:293:5:293:33 | call to super | super | 0 | calls.rb:293:11:293:11 | 6 | -| calls.rb:293:5:293:33 | call to super | super | 1 | calls.rb:293:14:293:14 | 7 | -callsWithReceiver -| calls.rb:11:1:11:7 | call to bar | calls.rb:11:1:11:3 | 123 | -| calls.rb:25:1:27:3 | call to bar | calls.rb:25:1:25:3 | 123 | -| calls.rb:101:1:101:9 | call to bar | calls.rb:101:1:101:3 | call to foo | -| calls.rb:102:1:102:9 | call to baz | calls.rb:102:1:102:3 | call to bar | | calls.rb:303:5:303:13 | call to super | calls.rb:303:5:303:7 | call to foo | | calls.rb:305:5:305:15 | call to super | calls.rb:305:5:305:9 | call to super | callsWithBlock diff --git a/ql/test/library-tests/ast/calls/calls.ql b/ql/test/library-tests/ast/calls/calls.ql index fb88970d3eb..93861bef7a1 100644 --- a/ql/test/library-tests/ast/calls/calls.ql +++ b/ql/test/library-tests/ast/calls/calls.ql @@ -8,10 +8,6 @@ query predicate callsWithNoReceiverArgumentsOrBlock(Call c, string name) { not exists(c.getBlock()) } -query predicate callsWithNameScopeExpr(Call c, Expr se) { se = c.getMethodNameScopeExpr() } - -query predicate callsWithGlobalNameScopeExpr(Call c) { c.methodNameHasGlobalScope() } - query predicate callsWithArguments(Call c, string name, int n, Expr argN) { name = c.getMethodName() and argN = c.getArgument(n)