diff --git a/CHANGELOG.md b/CHANGELOG.md index 4254c93a..b078200f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ### 0.9.18-dev +* The `lobster-python` tool adds the counter logic to the function + identifier. This improves the situations where different functions have + the same name. Line numbers are no longer used in the identifier. + * The `lobster-codebeamer` tool now supports `refs` as an upstream reference * The `lobster-online-report` tool now works with config files located in diff --git a/lobster/tools/python/python.py b/lobster/tools/python/python.py index 1e545f4c..496bff68 100755 --- a/lobster/tools/python/python.py +++ b/lobster/tools/python/python.py @@ -22,6 +22,7 @@ import os.path import multiprocessing import functools +import re from libcst.metadata import PositionProvider import libcst as cst @@ -32,6 +33,28 @@ 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): @@ -197,13 +220,21 @@ 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 diff --git a/test-system/lobster-python/multiple_identical_function_names.output b/test-system/lobster-python/multiple_identical_function_names.output new file mode 100644 index 00000000..a09f9df2 Binary files /dev/null and b/test-system/lobster-python/multiple_identical_function_names.output differ diff --git a/test-system/lobster-python/multiple_identical_function_names.py b/test-system/lobster-python/multiple_identical_function_names.py new file mode 100644 index 00000000..7e6e9e7b --- /dev/null +++ b/test-system/lobster-python/multiple_identical_function_names.py @@ -0,0 +1,28 @@ +flag = False +requirements_global = None + +if flag == True: + def get_requirements(requirements): + return requirements +else: + def set_requirements(requirements): + requirements_global = requirements + return requirements_global + + +def get_requirements(requirements): + return requirements + + +def display_requirements(): + print(get_requirements()) + + +def set_requirements(requirements): + requirements_global = requirements + return requirements_global + + +def get_requirements(requirements): + return requirements +