Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit bd85c8e

Browse files
committed
Added function error predicate
1 parent dddb34a commit bd85c8e

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
3+
4+
def f(x: int):
5+
if x < 0:
6+
raise ValueError("x must be non-negative")
7+
else:
8+
return x
9+
10+
11+
def g(x: int):
12+
if x < 0:
13+
return -x
14+
else:
15+
return x
16+
17+
18+
if __name__ == "__main__":
19+
x_ = int(sys.argv[1])
20+
g(x_)
21+
f(x_)

src/sflkit/analysis/factory.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
ContainsDigitPredicate,
1919
ContainsSpecialPredicate,
2020
EmptyBytesPredicate,
21+
FunctionErrorPredicate,
2122
)
2223
from sflkit.analysis.spectra import Line, Function, Loop, DefUse, Length
2324
from sflkit.model.scope import Scope
@@ -429,6 +430,29 @@ def get_analysis(self, event, scope: Scope = None) -> List[AnalysisObject]:
429430
return self.objects[key][:]
430431

431432

433+
class FunctionErrorFactory(AnalysisFactory):
434+
def __init__(self):
435+
super().__init__()
436+
self.function_mapping = dict()
437+
438+
def get_analysis(self, event, scope: Scope = None) -> List[AnalysisObject]:
439+
if event.event_type == EventType.FUNCTION_ENTER:
440+
self.function_mapping[event.function_id] = event.line
441+
if event.event_type in (EventType.FUNCTION_ERROR, EventType.FUNCTION_EXIT):
442+
line = self.function_mapping[event.function_id]
443+
key = (
444+
FunctionErrorPredicate.analysis_type(),
445+
event.file,
446+
line,
447+
event.function_id,
448+
)
449+
if key not in self.objects:
450+
self.objects[key] = FunctionErrorPredicate(
451+
event.file, line, event.function
452+
)
453+
return [self.objects[key]]
454+
455+
432456
analysis_factory_mapping = {
433457
AnalysisType.LINE: LineFactory,
434458
AnalysisType.BRANCH: BranchFactory,
@@ -446,4 +470,5 @@ def get_analysis(self, event, scope: Scope = None) -> List[AnalysisObject]:
446470
AnalysisType.VARIABLE: VariableFactory,
447471
AnalysisType.SCALAR_PAIR: ScalarPairFactory,
448472
AnalysisType.FUNCTION: FunctionFactory,
473+
AnalysisType.FUNCTION_ERROR: FunctionErrorFactory,
449474
}

src/sflkit/analysis/mapping.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
VariablePredicate,
77
ReturnPredicate,
88
NonePredicate,
9+
FunctionErrorPredicate,
910
)
1011
from sflkit.analysis.predicate import (
1112
EmptyStringPredicate,
@@ -39,6 +40,7 @@
3940
analysis_mapping[AnalysisType.DIGIT_STRING] = ContainsDigitPredicate
4041
analysis_mapping[AnalysisType.SPECIAL_STRING] = ContainsSpecialPredicate
4142
analysis_mapping[AnalysisType.CONDITION] = Condition
43+
analysis_mapping[AnalysisType.FUNCTION_ERROR] = FunctionErrorPredicate
4244

4345
"""
4446
If you want to add new spectra or predicates, please register them here and in sdtools/analysis/analysis_type.py

src/sflkit/language/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self):
6161
self.random = random.randbytes(4).hex()
6262

6363
def get_var_name(self):
64-
var = f"sd_tmp_{self.random}_{self._tmp_count}"
64+
var = f"sk_tmp_{self.random}_{self._tmp_count}"
6565
self._tmp_count += 1
6666
return var
6767

src/sflkit/language/python/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def visit_function(
521521
function_error_event = FunctionErrorEvent(
522522
self.file,
523523
node.lineno,
524-
self.get_function_event_id(node, self.event_id_generator),
524+
self.event_id_generator.get_next_id(),
525525
node.name,
526526
self.get_function_id(node),
527527
)

src/sflkit/mapping.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ def get_path(identifier: str):
2727
def load(config: Any):
2828
if not hasattr(config, "identifier"):
2929
raise InstrumentationError(f"Argument does not have an identifier")
30-
file = EventMapping.get_path(config.identifier())
30+
return EventMapping.load_from_file(config.identifier())
31+
32+
@staticmethod
33+
def load_from_file(identifier: str):
34+
file = EventMapping.get_path(identifier)
3135
if file.exists():
3236
return EventMapping(load_json(file))
3337
else:
3438
raise InstrumentationError(
35-
f"Cannot find information about instrumentation of {config.identifier()}"
39+
f"Cannot find information about instrumentation of {identifier or file}"
3640
)
3741

3842
def write(self, config):

0 commit comments

Comments
 (0)