From 9d4230ac069ae8be2aec6c56cf14698159ffdc07 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Thu, 24 Oct 2024 16:15:51 +0200 Subject: [PATCH 1/8] add into metadata info all reachable functions --- vyper/compiler/output.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index f5f99a0bc3..649ed6c772 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -18,6 +18,7 @@ from vyper.typing import StorageLayout from vyper.utils import vyper_warn from vyper.warnings import ContractSizeLimitWarning +from vyper.semantics.types.function import ContractFunctionT def build_ast_dict(compiler_data: CompilerData) -> dict: @@ -206,7 +207,15 @@ def build_ir_runtime_dict_output(compiler_data: CompilerData) -> dict: def build_metadata_output(compiler_data: CompilerData) -> dict: - sigs = compiler_data.function_signatures + _ = compiler_data.function_signatures + module_t = compiler_data.annotated_vyper_module._metadata["type"] + sigs = dict[str, ContractFunctionT]() + + for fn_t in module_t.exposed_functions: + assert isinstance(fn_t.ast_def, vy_ast.FunctionDef) + for inf_t in fn_t.reachable_internal_functions: + sigs[inf_t.name] = inf_t + sigs[fn_t.name] = fn_t def _var_rec_dict(variable_record): ret = vars(variable_record).copy() From 7c6930ece89fe1d37ca130d1682443e89b2458b8 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Thu, 24 Oct 2024 19:53:08 +0200 Subject: [PATCH 2/8] add tests --- .../cli/vyper_json/test_output_selection.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/unit/cli/vyper_json/test_output_selection.py b/tests/unit/cli/vyper_json/test_output_selection.py index f7fbfe673c..b2d92652ce 100644 --- a/tests/unit/cli/vyper_json/test_output_selection.py +++ b/tests/unit/cli/vyper_json/test_output_selection.py @@ -4,6 +4,7 @@ from vyper.cli.vyper_json import TRANSLATE_MAP, get_output_formats from vyper.exceptions import JSONError +from vyper import compiler def test_no_outputs(): @@ -76,3 +77,37 @@ def test_solc_style(): def test_metadata(): input_json = {"sources": {"foo.vy": ""}, "settings": {"outputSelection": {"*": ["metadata"]}}} assert get_output_formats(input_json) == {PurePath("foo.vy"): ["metadata"]} + +def test_metadata_contain_all_reachable_functions(make_input_bundle): + code_a = """ +@internal +def foo() -> uint256: + return 43 + +@internal +def faa() -> uint256: + return 76 + """ + + code_b = """ +import A + +@internal +def foochino() -> uint256: + return 43 + +@external +def bar(): + self.foochino() + A.foo() + assert 1 != 12 + """ + + input_bundle = make_input_bundle({"A.vy": code_a, "B.vy": code_b}) + + out = compiler.compile_code(code_b, input_bundle=input_bundle, output_formats=["metadata"])["metadata"] + print(out) + assert "foochino" in out["function_info"] + assert "bar" in out["function_info"] + assert "foo" in out["function_info"] + assert "faa" not in out["function_info"] From 5f970389b26f4a8c48ef6042b3151a2a4fa2c197 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Thu, 24 Oct 2024 19:53:39 +0200 Subject: [PATCH 3/8] lint --- tests/unit/cli/vyper_json/test_output_selection.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unit/cli/vyper_json/test_output_selection.py b/tests/unit/cli/vyper_json/test_output_selection.py index b2d92652ce..3f6897a239 100644 --- a/tests/unit/cli/vyper_json/test_output_selection.py +++ b/tests/unit/cli/vyper_json/test_output_selection.py @@ -78,6 +78,7 @@ def test_metadata(): input_json = {"sources": {"foo.vy": ""}, "settings": {"outputSelection": {"*": ["metadata"]}}} assert get_output_formats(input_json) == {PurePath("foo.vy"): ["metadata"]} + def test_metadata_contain_all_reachable_functions(make_input_bundle): code_a = """ @internal @@ -105,7 +106,9 @@ def bar(): input_bundle = make_input_bundle({"A.vy": code_a, "B.vy": code_b}) - out = compiler.compile_code(code_b, input_bundle=input_bundle, output_formats=["metadata"])["metadata"] + out = compiler.compile_code(code_b, input_bundle=input_bundle, output_formats=["metadata"])[ + "metadata" + ] print(out) assert "foochino" in out["function_info"] assert "bar" in out["function_info"] From 5bfb3774e6a1d587900df7c1231a293d5af91884 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Sun, 27 Oct 2024 16:46:36 +0100 Subject: [PATCH 4/8] add qualified name to internal functions --- .../cli/vyper_json/test_output_selection.py | 22 ++++++++++++------- vyper/compiler/output.py | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/unit/cli/vyper_json/test_output_selection.py b/tests/unit/cli/vyper_json/test_output_selection.py index 3f6897a239..cc9dd32923 100644 --- a/tests/unit/cli/vyper_json/test_output_selection.py +++ b/tests/unit/cli/vyper_json/test_output_selection.py @@ -94,12 +94,12 @@ def faa() -> uint256: import A @internal -def foochino() -> uint256: +def foo() -> uint256: return 43 @external def bar(): - self.foochino() + self.foo() A.foo() assert 1 != 12 """ @@ -108,9 +108,15 @@ def bar(): out = compiler.compile_code(code_b, input_bundle=input_bundle, output_formats=["metadata"])[ "metadata" - ] - print(out) - assert "foochino" in out["function_info"] - assert "bar" in out["function_info"] - assert "foo" in out["function_info"] - assert "faa" not in out["function_info"] + ]["function_info"] + + def has_suffix_key(data: dict, suffix: str) -> bool: + for key in data.keys(): + if key.endswith(suffix): + return True + return False + + assert has_suffix_key(out, ": foo") + assert has_suffix_key(out, "bar") + assert has_suffix_key(out, "A.vy: foo") + assert not has_suffix_key(out, "faa") diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index 649ed6c772..cfe0f13f93 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -213,8 +213,8 @@ def build_metadata_output(compiler_data: CompilerData) -> dict: for fn_t in module_t.exposed_functions: assert isinstance(fn_t.ast_def, vy_ast.FunctionDef) - for inf_t in fn_t.reachable_internal_functions: - sigs[inf_t.name] = inf_t + for rif_t in fn_t.reachable_internal_functions: + sigs[rif_t.ast_def.module_node.path + ": " + rif_t.name] = rif_t sigs[fn_t.name] = fn_t def _var_rec_dict(variable_record): From f4c34a84f0f8e8067f71e3f24540fb384743e7a9 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Tue, 29 Oct 2024 09:31:42 +0100 Subject: [PATCH 5/8] add comment --- vyper/compiler/output.py | 1 + 1 file changed, 1 insertion(+) diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index cfe0f13f93..a216a0fa2f 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -207,6 +207,7 @@ def build_ir_runtime_dict_output(compiler_data: CompilerData) -> dict: def build_metadata_output(compiler_data: CompilerData) -> dict: + # need ir info to be computed _ = compiler_data.function_signatures module_t = compiler_data.annotated_vyper_module._metadata["type"] sigs = dict[str, ContractFunctionT]() From 1b771aadb4b85229031d3b369f678933bea531b9 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Tue, 29 Oct 2024 12:57:43 +0100 Subject: [PATCH 6/8] fix isort errors --- tests/unit/cli/vyper_json/test_output_selection.py | 2 +- vyper/compiler/output.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/unit/cli/vyper_json/test_output_selection.py b/tests/unit/cli/vyper_json/test_output_selection.py index cc9dd32923..a00b43aa75 100644 --- a/tests/unit/cli/vyper_json/test_output_selection.py +++ b/tests/unit/cli/vyper_json/test_output_selection.py @@ -2,9 +2,9 @@ import pytest +from vyper import compiler from vyper.cli.vyper_json import TRANSLATE_MAP, get_output_formats from vyper.exceptions import JSONError -from vyper import compiler def test_no_outputs(): diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index a216a0fa2f..06a7af40c7 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -13,12 +13,11 @@ from vyper.exceptions import VyperException from vyper.ir import compile_ir from vyper.semantics.analysis.base import ModuleInfo -from vyper.semantics.types.function import FunctionVisibility, StateMutability +from vyper.semantics.types.function import ContractFunctionT, FunctionVisibility, StateMutability from vyper.semantics.types.module import InterfaceT from vyper.typing import StorageLayout from vyper.utils import vyper_warn from vyper.warnings import ContractSizeLimitWarning -from vyper.semantics.types.function import ContractFunctionT def build_ast_dict(compiler_data: CompilerData) -> dict: From 1170e279abeb7f5f465e543d6be75b0f222cda6c Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Sat, 23 Nov 2024 11:09:27 +0000 Subject: [PATCH 7/8] replace path by ModuleT._id --- vyper/compiler/output.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index 06a7af40c7..1c73412aee 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -214,7 +214,7 @@ def build_metadata_output(compiler_data: CompilerData) -> dict: for fn_t in module_t.exposed_functions: assert isinstance(fn_t.ast_def, vy_ast.FunctionDef) for rif_t in fn_t.reachable_internal_functions: - sigs[rif_t.ast_def.module_node.path + ": " + rif_t.name] = rif_t + sigs[rif_t.ast_def.module_node._metadata["type"]._id + ": " + rif_t.name] = rif_t sigs[fn_t.name] = fn_t def _var_rec_dict(variable_record): From 4a95adbcfc890fe7d3f7d9d49fe0fc2c6dbb4d78 Mon Sep 17 00:00:00 2001 From: Sand Bubbles Date: Sun, 24 Nov 2024 11:28:16 +0000 Subject: [PATCH 8/8] change prefix of internal functions to their ir id --- tests/unit/cli/vyper_json/test_output_selection.py | 4 ++-- vyper/compiler/output.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/cli/vyper_json/test_output_selection.py b/tests/unit/cli/vyper_json/test_output_selection.py index a00b43aa75..3e1bc45494 100644 --- a/tests/unit/cli/vyper_json/test_output_selection.py +++ b/tests/unit/cli/vyper_json/test_output_selection.py @@ -116,7 +116,7 @@ def has_suffix_key(data: dict, suffix: str) -> bool: return True return False - assert has_suffix_key(out, ": foo") + assert has_suffix_key(out, "0: foo") assert has_suffix_key(out, "bar") - assert has_suffix_key(out, "A.vy: foo") + assert has_suffix_key(out, "1: foo") assert not has_suffix_key(out, "faa") diff --git a/vyper/compiler/output.py b/vyper/compiler/output.py index 1c73412aee..ef63c6839a 100644 --- a/vyper/compiler/output.py +++ b/vyper/compiler/output.py @@ -214,7 +214,7 @@ def build_metadata_output(compiler_data: CompilerData) -> dict: for fn_t in module_t.exposed_functions: assert isinstance(fn_t.ast_def, vy_ast.FunctionDef) for rif_t in fn_t.reachable_internal_functions: - sigs[rif_t.ast_def.module_node._metadata["type"]._id + ": " + rif_t.name] = rif_t + sigs[str(rif_t._ir_info.func_t._function_id) + ": " + rif_t.name] = rif_t sigs[fn_t.name] = fn_t def _var_rec_dict(variable_record):