Merge pull request #964 from markshannon/python-locations-for-packages

Python: Make sure packages have locations.
This commit is contained in:
Taus
2019-02-26 11:55:27 +01:00
committed by GitHub
7 changed files with 18 additions and 4 deletions

View File

@@ -391,10 +391,14 @@ class Location extends @location {
/** Gets the file for this location */
File getFile() {
result = this.getPath()
}
private Container getPath() {
locations_default(this, result, _, _, _, _)
or
exists(Module m | locations_ast(this, m, _, _, _, _) |
result = m.getFile()
result = m.getPath()
)
}
@@ -423,7 +427,7 @@ class Location extends @location {
}
string toString() {
result = this.getFile().getName() + ":" + this.getStartLine().toString()
result = this.getPath().getName() + ":" + this.getStartLine().toString()
}
predicate hasLocationInfo(string filepath, int bl, int bc, int el, int ec) {

View File

@@ -115,6 +115,9 @@ class Module extends Module_, Scope, AstNode {
override Location getLocation() {
py_scope_location(result, this)
or
not py_scope_location(_, this) and
locations_ast(result, this, 0, 0, 0, 0)
}
/** Gets a child module or package of this package */

View File

@@ -403,6 +403,7 @@
| i_imports.py:18 | ControlFlowNode for sys | Module sys | builtin-class module | 17 | import |
| i_imports.py:23 | ControlFlowNode for ImportExpr | Module code | builtin-class module | 23 | import |
| i_imports.py:23 | ControlFlowNode for code | Module code | builtin-class module | 23 | import |
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package | builtin-class module | 0 | import |
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package.x | builtin-class module | 0 | import |
| i_imports.py:24 | ControlFlowNode for code | Module code | builtin-class module | 23 | import |
| i_imports.py:27 | ControlFlowNode for ImportExpr | Module code.test_package | builtin-class module | 27 | import |

View File

@@ -512,6 +512,7 @@
| i_imports.py:18 | ControlFlowNode for sys | Module sys | builtin-class module | 17 |
| i_imports.py:23 | ControlFlowNode for ImportExpr | Module code | builtin-class module | 23 |
| i_imports.py:23 | ControlFlowNode for code | Module code | builtin-class module | 23 |
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package | builtin-class module | 0 |
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package.x | builtin-class module | 0 |
| i_imports.py:24 | ControlFlowNode for code | Module code | builtin-class module | 23 |
| i_imports.py:27 | ControlFlowNode for ImportExpr | Module code.test_package | builtin-class module | 27 |

View File

@@ -95,9 +95,11 @@ predicate ssa_sanity(string clsname, string problem, string what) {
not exists(m.getName()) and
problem = "does not have a name"
or
not m.isPackage() and
not exists(Variable v | v.getId() = "__name__" and v.getScope() = m) and
problem = "does not have a __name__ variable"
or
not m.isPackage() and
not exists(PyNodeDefinition def |
def.getDefiningNode().getScope() = m and
def.getVariable().getName() = "__name__"

View File

@@ -21,16 +21,18 @@ class DerivedClass(BaseClass):
self.inst_attr
self.shadowing
#ODASA-3836
def comprehensions_and_generators(seq):
[y*y for y in seq]
(y*y for y in seq)
{y*y for y in seq}
{y:y*y for y in seq}
#ODASA-5391
@decorator(x)
class Decorated(object):
pass
d = Decorated()
import package