Skip to content

Commit

Permalink
lobster-python now uses counter logic.
Browse files Browse the repository at this point in the history
The identifier now uses counter if there exists multiple functions with
same name.

Issue #58
  • Loading branch information
kedarnn authored and phiwuu committed Sep 16, 2024
1 parent 9c9d863 commit 90d4410
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

### 0.9.17

* The `lobster-python` tool now adds the line number to the function
* The `lobster-python` tool now adds the counter logic to the function
identifier. This supports situations where different functions have
the same name

Expand Down
31 changes: 29 additions & 2 deletions lobster/tools/python/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os.path
import multiprocessing
import functools
import re

from libcst.metadata import PositionProvider
import libcst as cst
Expand All @@ -32,6 +33,26 @@

LOBSTER_TRACE_PREFIX = "# lobster-trace: "
LOBSTER_JUST_PREFIX = "# lobster-exclude: "
func_name = []


def count_occurrence_of_last_function_from_function_name_list(function_names):
"""
Function returns a function name(last function from the list of function names)
and its occurrence from the given list of function names
Example: function_names = ['hello.add:2', 'hello.sub:5', 'hello.add:8']
returns : hello.add-2
"""
function_and_file_name = re.split(r"[.:]", function_names[-1])
filename = function_and_file_name[0]
last_function = function_and_file_name[1]
count = 0
for element in range(0, len(function_names)-1):
if re.split(r"[.:]", function_names[element])[1] == last_function:
count += 1
function_name = filename + "." + last_function + ("-" + str(count) if count > 0 else '')

return function_name


def parse_value(val):
Expand Down Expand Up @@ -197,13 +218,19 @@ def to_lobster(self, schema, items):
assert schema is Implementation or schema is Activity
assert isinstance(items, list)

func_name.append(self.fqn())
tagname = count_occurrence_of_last_function_from_function_name_list(func_name)
pattern = r"[-]"
val = re.split(pattern, tagname)
name_value = val[0]

if schema is Implementation:
l_item = Implementation(tag = Tracing_Tag("python",
self.fqn()),
tagname),
location = self.location,
language = "Python",
kind = self.kind,
name = self.fqn())
name = name_value)
else:
if not self.name.startswith("test"):
return
Expand Down
87 changes: 87 additions & 0 deletions test-system/lobster-python/hello.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"data": [
{
"tag": "python hello.hello_world",
"location": {
"kind": "file",
"file": "./demoPython/hello.py",
"line": 2,
"column": null
},
"name": "hello.hello_world",
"messages": [],
"just_up": [],
"just_down": [],
"just_global": [],
"language": "Python",
"kind": "Function"
},
{
"tag": "python hello.hello_world-2",
"location": {
"kind": "file",
"file": "./demoPython/hello.py",
"line": 5,
"column": null
},
"name": "hello.hello_world",
"messages": [],
"just_up": [],
"just_down": [],
"just_global": [],
"language": "Python",
"kind": "Function"
},
{
"tag": "python hello.hello_universe",
"location": {
"kind": "file",
"file": "./demoPython/hello.py",
"line": 8,
"column": null
},
"name": "hello.hello_universe",
"messages": [],
"just_up": [],
"just_down": [],
"just_global": [],
"language": "Python",
"kind": "Function"
},
{
"tag": "python hello.hello_world-3",
"location": {
"kind": "file",
"file": "./demoPython/hello.py",
"line": 11,
"column": null
},
"name": "hello.hello_world",
"messages": [],
"just_up": [],
"just_down": [],
"just_global": [],
"language": "Python",
"kind": "Function"
},
{
"tag": "python hello.hello_universe-2",
"location": {
"kind": "file",
"file": "./demoPython/hello.py",
"line": 14,
"column": null
},
"name": "hello.hello_universe",
"messages": [],
"just_up": [],
"just_down": [],
"just_global": [],
"language": "Python",
"kind": "Function"
}
],
"generator": "lobster_python",
"schema": "lobster-imp-trace",
"version": 3
}
18 changes: 18 additions & 0 deletions test-system/lobster-python/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if True:
def hello_world():
return 1
else:
def hello_world():
return 2

def hello_universe():
return 3

def hello_world():
return 10

def hello_universe():
return 12


print(hello_world())

0 comments on commit 90d4410

Please sign in to comment.