mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Python: address more review comments
This commit is contained in:
@@ -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 = "" }
|
||||
|
||||
|
||||
@@ -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.", "") +
|
||||
|
||||
Reference in New Issue
Block a user