Merge pull request #3924 from RasmusWL/python-metrics-queries-for-dist-compare

Approved by tausbn
This commit is contained in:
semmle-qlci 2020-07-14 13:03:02 +01:00 коммит произвёл GitHub
Родитель ee13e87f3b ee42d0839e
Коммит f8c03dcae6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 148 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,26 @@
/**
* Helpers for generating meta metrics, that is, metrics about the CodeQL analysis and extractor.
*/
import python
private import semmle.python.filters.GeneratedCode
private import semmle.python.filters.Tests
/**
* Gets the root folder of the snapshot.
*
* This is selected as the location for project-wide metrics.
*/
Folder projectRoot() { result.getRelativePath() = "" }
/** A file we ignore because it is a test file, part of a third-party library, or compiled/generated/bundled code. */
class IgnoredFile extends File {
IgnoredFile() {
any(TestScope ts).getLocation().getFile() = this
or
this instanceof GeneratedFile
or
// outside source root (inspired by `Scope.inSource`)
not exists(this.getRelativePath())
}
}

Просмотреть файл

@ -0,0 +1,65 @@
/**
* Provides predicates for measuring the quality of the call graph, that is,
* the number of calls that could be resolved to a callee.
*/
import python
import meta.MetaMetrics
/**
* A call that is (possibly) relevant for analysis quality.
* See `IgnoredFile` for details on what is excluded.
*/
class RelevantCall extends Call {
RelevantCall() { not this.getLocation().getFile() instanceof IgnoredFile }
}
/** Provides classes for call-graph resolution by using points-to. */
module PointsToBasedCallGraph {
/** A call that can be resolved by points-to. */
class ResolvableCall extends RelevantCall {
Value callee;
ResolvableCall() { callee.getACall() = this.getAFlowNode() }
/** Gets a resolved callee of this call. */
Value getCallee() { result = callee }
}
/** A call that cannot be resolved by points-to. */
class UnresolvableCall extends RelevantCall {
UnresolvableCall() { not this instanceof ResolvableCall }
}
/**
* A call that can be resolved by points-to, where the resolved callee is relevant.
* Relevant callees include:
* - builtins
* - standard library
* - source code of the project
*/
class ResolvableCallRelevantCallee extends ResolvableCall {
ResolvableCallRelevantCallee() {
callee.isBuiltin()
or
exists(File file |
file = callee.(CallableValue).getScope().getLocation().getFile()
or
file = callee.(ClassValue).getScope().getLocation().getFile()
|
file.inStdlib()
or
// part of the source code of the project
exists(file.getRelativePath())
)
}
}
/**
* A call that can be resolved by points-to, where the resolved callee is not considered relevant.
* See `ResolvableCallRelevantCallee` for the definition of relevance.
*/
class ResolvableCallIrrelevantCallee extends ResolvableCall {
ResolvableCallIrrelevantCallee() { not this instanceof ResolvableCallRelevantCallee }
}
}

Просмотреть файл

@ -0,0 +1,15 @@
/**
* @name Ratio of resolvable call by points-to
* @description The percentage of (relevant) calls that can be resolved to a callee.
* @kind metric
* @metricType project
* @metricAggregate sum min max avg
* @tags meta
* @id py/meta/points-to-resolvable-call-ratio
*/
import python
import CallGraphQuality
select projectRoot(),
100.0 * count(PointsToBasedCallGraph::ResolvableCall call) / count(RelevantCall call).(float)

Просмотреть файл

@ -0,0 +1,14 @@
/**
* @name Resolvable calls by points-to
* @description The number of (relevant) calls that can be resolved to a callee.
* @kind metric
* @metricType project
* @metricAggregate sum
* @tags meta
* @id py/meta/points-to-resolvable-calls
*/
import python
import CallGraphQuality
select projectRoot(), count(PointsToBasedCallGraph::ResolvableCall call)

Просмотреть файл

@ -0,0 +1,14 @@
/**
* @name Resolvable calls by points-to, to relevant callee
* @description The number of (relevant) calls that could be resolved to a callee that is relevant.
* @kind metric
* @metricType project
* @metricAggregate sum
* @tags meta
* @id py/meta/points-to-resolvable-calls-relevant-callee
*/
import python
import CallGraphQuality
select projectRoot(), count(PointsToBasedCallGraph::ResolvableCallRelevantCallee call)

Просмотреть файл

@ -0,0 +1,14 @@
/**
* @name Resolvable call candidates
* @description The number of (relevant) calls in the program.
* @kind metric
* @metricType project
* @metricAggregate sum
* @tags meta
* @id py/meta/resolvable-call-candidates
*/
import python
import CallGraphQuality
select projectRoot(), count(RelevantCall call)