Python: Remove points-to from Module

This commit is contained in:
Taus
2025-10-30 12:04:31 +00:00
parent b93ce98612
commit 820d8e76c4
4 changed files with 21 additions and 12 deletions

View File

@@ -21,6 +21,7 @@
private import python
private import semmle.python.pointsto.PointsTo
private import semmle.python.objects.Modules
/**
* An extension of `ControlFlowNode` that provides points-to predicates.
@@ -191,3 +192,19 @@ class ExprWithPointsTo extends Expr {
override string getAQlClass() { none() }
}
/**
* An extension of `Module` that provides points-to related methods.
*/
class ModuleWithPointsTo extends Module {
/** Gets a name exported by this module, that is the names that will be added to a namespace by 'from this-module import *' */
string getAnExport() {
py_exports(this, result)
or
exists(ModuleObjectInternal mod | mod.getSource() = this.getEntryNode() |
mod.(ModuleValue).exports(result)
)
}
override string getAQlClass() { none() }
}

View File

@@ -1,5 +1,4 @@
import python
private import semmle.python.objects.Modules
private import semmle.python.internal.CachedStages
/**
@@ -66,15 +65,6 @@ class Module extends Module_, Scope, AstNode {
/** Whether this module is a package initializer */
predicate isPackageInit() { this.getName().matches("%\\_\\_init\\_\\_") and not this.isPackage() }
/** Gets a name exported by this module, that is the names that will be added to a namespace by 'from this-module import *' */
string getAnExport() {
py_exports(this, result)
or
exists(ModuleObjectInternal mod | mod.getSource() = this.getEntryNode() |
mod.(ModuleValue).exports(result)
)
}
/** Gets the source file for this module */
File getFile() { py_module_path(this, result) }

View File

@@ -13,6 +13,7 @@
*/
import python
private import LegacyPointsTo
import Definition
/**
@@ -58,7 +59,7 @@ predicate unused_global(Name unused, GlobalVariable v) {
// indirectly
defn.getBasicBlock().reachesExit() and u.getScope() != unused.getScope()
) and
not unused.getEnclosingModule().getAnExport() = v.getId() and
not unused.getEnclosingModule().(ModuleWithPointsTo).getAnExport() = v.getId() and
not exists(unused.getParentNode().(ClassDef).getDefinedClass().getADecorator()) and
not exists(unused.getParentNode().(FunctionDef).getDefinedFunction().getADecorator()) and
unused.defines(v) and

View File

@@ -1,4 +1,5 @@
import python
private import LegacyPointsTo
from Module m
from ModuleWithPointsTo m
select m.toString(), m.getAnExport().toString()