Skip to content

Commit

Permalink
add line/col to tests for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
yinwang0 committed Nov 10, 2016
1 parent 4cf5ed4 commit 9e8351c
Show file tree
Hide file tree
Showing 28 changed files with 33,891 additions and 331 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/yinwang/pysonar/Binding.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public enum Kind {
// fields from Def
public int start = -1;
public int end = -1;
public int line = -1;
public int col = -1;
public int bodyStart = -1;
public int bodyEnd = -1;

Expand Down Expand Up @@ -80,6 +82,8 @@ public Binding(@NotNull String id, @NotNull Node node, @NotNull Type type, @NotN
private void initLocationInfo(Node node) {
start = node.start;
end = node.end;
line = node.line;
col = node.col;

Node parent = node.parent;
if ((parent instanceof FunctionDef && ((FunctionDef) parent).name == node) ||
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/yinwang/pysonar/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Node convert(Object o) {
int start = startDouble == null ? 0 : startDouble.intValue();
int end = endDouble == null ? 1 : endDouble.intValue();
int line = lineDouble == null ? 1 : lineDouble.intValue();
int col = colDouble == null ? 1 : colDouble.intValue();
int col = colDouble == null ? 1 : colDouble.intValue() + 1;


if (type.equals("Module")) {
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/org/yinwang/pysonar/TestInference.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public void generateRefs(Analyzer analyzer)
ref.put("file", filename);
ref.put("start", e.getKey().start);
ref.put("end", e.getKey().end);
ref.put("line", e.getKey().line);
ref.put("col", e.getKey().col);

List<Map<String, Object>> dests = new ArrayList<>();
Collections.sort(bindings, (a, b) -> a.start == b.start ? a.end - b.end : a.start - b.start);
Expand All @@ -84,6 +86,8 @@ public void generateRefs(Analyzer analyzer)
dest.put("file", destFile);
dest.put("start", b.start);
dest.put("end", b.end);
dest.put("line", b.line);
dest.put("col", b.col);
dest.put("type", b.type.toString());
dests.add(dest);
}
Expand Down Expand Up @@ -125,21 +129,23 @@ public boolean checkRefs(Analyzer analyzer)
{
String name1 = (String) refMap.get("name");
String file1 = (String) refMap.get("file");
int start1 = (int) Math.floor((double) refMap.get("start"));
int end1 = (int) Math.floor((double) refMap.get("end"));
int line1 = (int) Math.floor((double) refMap.get("line"));
int col1 = (int) Math.floor((double) refMap.get("col"));
String[] type1 = new String[1];

String fileShort2 = (String) d.get("file");
String file2 = $.projAbsPath(fileShort2);
int start2 = (int) Math.floor((double) d.get("start"));
int end2 = (int) Math.floor((double) d.get("end"));
int line2 = (int) Math.floor((double) d.get("line"));
int col2 = (int) Math.floor((double) d.get("col"));
String type2 = (String) d.get("type");

if (!checkExist(actual, file2, start2, end2))
{
String variable = name1 + ":" + start1 + ":" + end1;
String loc = name1 + ":" + start2 + ":" + end2;
if (!file1.equals(file2))
String variable = name1 + ":" + line1 + ":" + col1;
String loc = name1 + ":" + line2 + ":" + col2;
if (!file1.equals(fileShort2))
{
loc = fileShort2 + ":" + loc;
}
Expand All @@ -148,9 +154,9 @@ public boolean checkRefs(Analyzer analyzer)
}
else if (!checkType(actual, file2, start2, end2, type2, type1))
{
String variable = name1 + ":" + start1 + ":" + end1;
String loc = name1 + ":" + start2 + ":" + end2;
if (!file1.equals(file2))
String variable = name1 + ":" + line1 + ":" + col1;
String loc = name1 + ":" + line2 + ":" + col2;
if (!file1.equals(fileShort2))
{
loc = fileShort2 + ":" + loc;
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/org/yinwang/pysonar/python/dump_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def improve_node(node, s):
elif isinstance(node, AST):
find_start(node, s)
find_end(node, s)
node.lineno, node.col_offset = map_line_col(node.start)
if hasattr(node, 'start'):
node.lineno, node.col_offset = map_line_col(node.start)
add_missing_names(node, s)

for f in node_fields(node):
Expand Down Expand Up @@ -509,7 +510,6 @@ def str_to_name(s, start):
name = Name(id1, None)
name.start = name_start
name.end = name_end
name.lineno, name.col_offset = map_line_col(name_start)
return name


Expand All @@ -531,7 +531,6 @@ def convert_ops(ops, s, start):
op_node = Name(syms[j], None)
op_node.start = i
op_node.end = i + oplen
op_node.lineno, op_node.col_offset = map_line_col(i)
ret.append(op_node)
j += 1
i = op_node.end
Expand Down
6 changes: 5 additions & 1 deletion tests/bom.test/refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"name": "x",
"file": "bom.py",
"start": 43,
"end": 44
"end": 44,
"line": 3,
"col": 20
},
"dests": [
{
"name": "x",
"file": "bom.py",
"start": 17,
"end": 18,
"line": 2,
"col": 1,
"type": "int"
}
]
Expand Down
46 changes: 39 additions & 7 deletions tests/call.test/refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"name": "baz1",
"file": "test1.py",
"start": 126,
"end": 130
"end": 130,
"line": 17,
"col": 1
},
"dests": [
{
"name": "baz1",
"file": "test1.py",
"start": 63,
"end": 67,
"line": 9,
"col": 5,
"type": "() -> int"
}
]
Expand All @@ -21,14 +25,18 @@
"name": "bar",
"file": "test1.py",
"start": 82,
"end": 85
"end": 85,
"line": 10,
"col": 12
},
"dests": [
{
"name": "bar",
"file": "test1.py",
"start": 31,
"end": 34,
"line": 5,
"col": 5,
"type": "str -> str / int -> int"
}
]
Expand All @@ -38,14 +46,18 @@
"name": "foo",
"file": "test1.py",
"start": 50,
"end": 53
"end": 53,
"line": 6,
"col": 12
},
"dests": [
{
"name": "foo",
"file": "test1.py",
"start": 4,
"end": 7,
"line": 1,
"col": 5,
"type": "str -> str / int -> int"
}
]
Expand All @@ -55,21 +67,27 @@
"name": "y",
"file": "test1.py",
"start": 54,
"end": 55
"end": 55,
"line": 6,
"col": 16
},
"dests": [
{
"name": "y",
"file": "test1.py",
"start": 35,
"end": 36,
"line": 5,
"col": 9,
"type": "int"
},
{
"name": "y",
"file": "test1.py",
"start": 35,
"end": 36,
"line": 5,
"col": 9,
"type": "str"
}
]
Expand All @@ -79,21 +97,27 @@
"name": "x",
"file": "test1.py",
"start": 23,
"end": 24
"end": 24,
"line": 2,
"col": 12
},
"dests": [
{
"name": "x",
"file": "test1.py",
"start": 8,
"end": 9,
"line": 1,
"col": 9,
"type": "int"
},
{
"name": "x",
"file": "test1.py",
"start": 8,
"end": 9,
"line": 1,
"col": 9,
"type": "str"
}
]
Expand All @@ -103,14 +127,18 @@
"name": "baz2",
"file": "test1.py",
"start": 133,
"end": 137
"end": 137,
"line": 18,
"col": 1
},
"dests": [
{
"name": "baz2",
"file": "test1.py",
"start": 95,
"end": 99,
"line": 13,
"col": 5,
"type": "() -> str"
}
]
Expand All @@ -120,14 +148,18 @@
"name": "bar",
"file": "test1.py",
"start": 114,
"end": 117
"end": 117,
"line": 14,
"col": 12
},
"dests": [
{
"name": "bar",
"file": "test1.py",
"start": 31,
"end": 34,
"line": 5,
"col": 5,
"type": "str -> str / int -> int"
}
]
Expand Down
Loading

0 comments on commit 9e8351c

Please sign in to comment.