Python: address more review comments

This commit is contained in:
Rasmus Lerchedahl Petersen
2024-06-27 16:05:21 +02:00
parent c2141b62e0
commit 27301edc28
4 changed files with 21 additions and 31 deletions

View File

@@ -22,14 +22,12 @@ class EndpointKind extends string {
*
* See `EndPointKind` for the possible kinds of elements.
*/
abstract class Endpoint instanceof Scope {
abstract class Endpoint instanceof Util::RelevantScope {
string namespace;
string type;
string name;
Endpoint() {
this.isPublic() and
this.getLocation().getFile() instanceof Util::RelevantFile and
exists(string scopePath, string path, int pathIndex |
scopePath = Util::computeScopePath(this) and
pathIndex = scopePath.indexOf(".", 0, 0)
@@ -62,14 +60,14 @@ abstract class Endpoint instanceof Scope {
Location getLocation() { result = super.getLocation() }
/** Gets the name of the class in which this endpoint is found, or the empty string if it is not found inside a class. */
string getType() { result = type }
string getClass() { result = type }
/**
* Gets the name of the endpoint if it is not a class, or the empty string if it is a class
*
* If this endpoint is a class, the class name can be obtained via `getType`.
*/
string getName() { result = name }
string getFunctionName() { result = name }
/**
* Gets a string representation of the parameters of this endpoint.
@@ -234,9 +232,9 @@ class FunctionEndpoint extends Endpoint instanceof Function {
* A class from source code.
*/
class ClassEndpoint extends Endpoint instanceof Class {
override string getType() { result = type + "." + name }
override string getClass() { result = type + "." + name }
override string getName() { result = "" }
override string getFunctionName() { result = "" }
override string getParameters() { result = "" }

View File

@@ -4,30 +4,20 @@
private import python
private import semmle.python.ApiGraphs
private import semmle.python.filters.Tests
/**
* A file that probably contains tests.
*/
class TestFile extends File {
TestFile() {
this.getRelativePath().regexpMatch(".*(test|spec|examples).+") and
not this.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
class RelevantScope extends Scope {
RelevantScope() {
this.isPublic() and
not this instanceof TestScope and
exists(this.getLocation().getFile().getRelativePath())
}
}
/**
* A file that is relevant in the context of library modeling.
*
* In practice, this means a file that is not part of test code.
*/
class RelevantFile extends File {
RelevantFile() { not this instanceof TestFile and not this.inStdlib() }
}
/**
* Gets the dotted path of a scope.
*/
string computeScopePath(Scope scope) {
string computeScopePath(RelevantScope scope) {
// base case
if scope instanceof Module
then
@@ -39,9 +29,8 @@ string computeScopePath(Scope scope) {
else
//recursive cases
if scope instanceof Class or scope instanceof Function
then
result = computeScopePath(scope.getEnclosingScope()) + "." + scope.getName()
else result = "unknown: " + scope.toString()
then result = computeScopePath(scope.getEnclosingScope()) + "." + scope.getName()
else result = "unknown: " + scope.toString()
}
signature predicate modelSig(string type, string path);
@@ -55,7 +44,7 @@ module FindModel<modelSig/2 model> {
/**
* Holds if the given scope has a model as identified by the provided predicate `model`.
*/
predicate hasModel(Scope scope) {
predicate hasModel(RelevantScope scope) {
exists(string type, string path, string searchPath | model(type, path) |
searchPath = possibleMemberPathPrefix(path, scope.getName()) and
pathToScope(scope, type, searchPath)
@@ -76,8 +65,7 @@ module FindModel<modelSig/2 model> {
* Holds if `(type,path)` identifies `scope`.
*/
bindingset[type, path]
predicate pathToScope(Scope scope, string type, string path) {
scope instanceof Endpoint
predicate pathToScope(RelevantScope scope, string type, string path) {
computeScopePath(scope) =
type.replaceAll("!", "") + "." +
path.replaceAll("Member[", "").replaceAll("]", "").replaceAll("Instance.", "") +